App Development » Objective-C Method for getting random CGPoints

Objective-C Method for getting random CGPoints

Here’s a pretty simple method for generating random CGPoints (great for using to randomize positions of enemies or obstacles on level load).

Just add this in your interface:

-(CGPoint) randomPoint;

And this in your implementation:

-(CGPoint) randomPoint
{
CGSize winSize = [CCDirector sharedDirector].winSize;

int maxY = winSize.height;
int maxX = winSize.width;

return ccp(arc4random() % maxX, arc4random() % maxY);
}

Enjoy!

Comments