Question

I've been following this code snippet from Ray Wenderlich's tutorials (http://codeviewer.org/view/code:1d8b). I'm trying to do almost the same thing, except I'm using a spritesheet (batchnode), and I want the targets/enemies to spawn from the right side of the screen (at any height), and move to the left side (at the same height). Here is my code. (http://codeviewer.org/view/code:1d8c).

Extra info - "moles" are my targets/enemies. Commented out code (//) beside the first few lines are just my notes. EDIT - Look at my comment on mjvotaw's answer.

Was it helpful?

Solution

If you want to place your mole sprite in a random spot along the right side, you should be able to just do:

[mole setPosition: ccp(winSize.width - [mole contentSize].width, arc4random() % winSize.height)];

That sets the mole at the far right edge, at a random spot along the y axis. Then, instead of actionMove using CCMoveTo, you can just use CCMoveBy, setting the position to ccp(-winSize.width, 0).

If this isn't what you're looking for, perhaps you should rethink how to ask your question.

OTHER TIPS

See this tutorial on creating enemies that move toward a target starting from a random point. It's a little ways down the page.

EDIT:

To create a random Y spawn point, do the following: (Assuming that you're using cocos2d)

// Define these at the top of the .m file
#define ARC4RANDOM_MAX 0x100000000
#define RAND_FLOAT ( (float)arc4random() / ARC4RANDOM_MAX )

- (CGPoint)pointWithRandomYAtX:(float)locationX
{
    CGSize size = [[CCDirector sharedDirector] winSize];
    return CGPointMake(locationX, RAND_FLOAT * size.height);
}

Then simply set the position of the "mole" to that point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top