Domanda

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 Wenderlich’s 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 I’m tremendously stuck! Below is my current 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 I’ve 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.

È stato utile?

Soluzione

Now, I haven't got Cocos2D in any of my projects, so don't know if there are any typos, but generally it should probably look a little something like the below. The idea is that all the comets should start at the same Y-position (off-screen) but have a randomized horizontal (x) position...

- (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]]];
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top