I am having real trouble getting a sprite to spawn on the screen at the top and then animate it from the top to the bottom. I have been following Ray Wenderlichs tutorial on creating a simple game however that moves the sprite from right to left, I would like it to move from top to bottom and now Im tremendously stuck! Below is my current code:
If anyone could point me in the right direction because Ive been stuck on this for quite a while and just cannot get the sprite to spawn randomly at the top of the screen and then animate down to the bottom. I have also looked at sample code such as tweejump but have had no luck.
Code:
- (void)addComet:(CCTime)dt
{
// Make sprite
CCSprite *comet = [CCSprite spriteWithImageNamed:@"PlayerSprite.png"];
// Verticle spawn range
int minY = comet.contentSize.width;
int maxY = self.contentSize.height - comet.contentSize.height / 2;
int rangeY = maxY - minY;
int randomY = (arc4random() % rangeY) + minY;
// Position comets slightly off the screen
comet.position = CGPointMake(self.contentSize.width + comet.contentSize.width, randomY);
[self addChild:comet];
// Duration range comets take to fly across screen
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int randomDuration = (arc4random() % rangeDuration) + minDuration;
// Give comet animation
CCAction *actionMove = [CCActionMoveTo actionWithDuration:randomDuration position:CGPointMake(0, 500)];
CCAction *actionRemove = [CCActionRemove action];
[comet runAction:[CCActionSequence actionWithArray:@[actionMove,actionRemove]]];
}
If anyone could point me in the right direction because Ive been stuck on this for quite a while and just cannot get the sprite to spawn randomly at the top of the screen and then animate down to the bottom. I have also looked at sample code such as tweejump but have had no luck.