现在,我的任何项目中都没有Cocos2D,所以不知道是否有错别字,但一般来说,它应该看起来有点像下面的样子。这个想法是所有的彗星都应该从
相同的
Y位置(屏幕外),但具有随机化的水平(x)位置。。。
- (void)addComet:(CCTime)dt
{
// Make sprite
CCSprite *comet = [CCSprite spriteWithImageNamed:@"PlayerSprite.png"];
NSInteger y = self.contentSize.height; // perhaps + comet.contentSize.height / 2, if the anchorPoint is 0.5, 0.5
// Random horizontal position
NSInteger maxX = self.contentSize.width;
NSInteger randomX = (arc4random() % maxX);
// Position comets slightly off the screen
comet.position = CGPointMake(randomX, y);
[self addChild:comet];
// Duration range comets take to fly across screen
NSInteger minDuration = 2.0;
NSInteger maxDuration = 4.0;
NSInteger rangeDuration = maxDuration - minDuration;
NSInteger randomDuration = (arc4random() % rangeDuration) + minDuration;
// Give comet animation
CCAction *actionMove = [CCActionMoveTo actionWithDuration:randomDuration position:CGPointMake(randomX, 0)]; // Moving it in a straight line vertically
CCAction *actionRemove = [CCActionRemove action];
[comet runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove]]];
}