2

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.

Joethemonkey101
  • 149
  • 2
  • 18
  • There's no errors or crashing, but the enemies spawn from the bottom left corner, and then go to ccp:(100, 100). They spawn from the wrong place. Also, they all spawn at the exact same place of that corner. It's not randomized. – Joethemonkey101 Aug 15 '11 at 00:13
  • I'm voting to close this question because the [links to codeviewer.org no longer work](https://meta.stackoverflow.com/questions/345443/what-should-happen-to-questions-using-codeviewer-org-for-sharing-code). Without these links, the question has no clear [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – g00glen00b Mar 16 '17 at 21:28

2 Answers2

0

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.

Daniel G. Wilson
  • 14,955
  • 2
  • 32
  • 39
  • That's the exact same tutorial I took the snippet from, and that's not my question. I know how/where I want to move the enemy, but the original spawning location is giving me trouble. – Joethemonkey101 Aug 15 '11 at 00:51
  • When I do that, the enemies spawn in the bottom left corner (wrong place), and then go off the screen to the left. When a new one spawns, an enemy flashes in a different/random location along the left side of the screen (what I want, except that it only flashes there for a split second, and I want it to be on the right side of the screen). Could you take a look at this file? It's my .m. Thanks http://www.4shared.com/file/zY8LSBiW/Mole.html The code is in the addMole method. – Joethemonkey101 Aug 15 '11 at 19:16
0

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.

mjvotaw
  • 396
  • 2
  • 9
  • Thanks for your help. I changed the code up a bit (http://codeviewer.org/view/code:1d91). When I run I get the enemy flashing on the position I want it to be at, but then switches locations to the bottom of the screen again. Is there something wrong with my code? Please check, thanks. – Joethemonkey101 Aug 15 '11 at 21:57
  • I noticed you set the mole's position on line 6, but you don't actually create the mole object until like 19, don't know how I didn't catch that before. You shouldn't be referencing the mole object before you create the CCSprite. – mjvotaw Aug 22 '11 at 19:08
  • THANKS so much! You don't know how much you've helped me out =D – Joethemonkey101 Aug 22 '11 at 20:32