开发者

Spawning Enemy at a Random Location [closed]

开发者 https://www.devze.com 2023-03-27 21:32 出处:网络
Closed. This question needs details or clarity. It is not currently accepting answers. 开发者_开发百科
Closed. This question needs details or clarity. It is not currently accepting answers.
开发者_开发百科

Want to improve this question? Add details and clarify the problem by editing this post.

Closed 6 years ago.

Improve this 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消