Show CCSprites with a delay

168 Views Asked by At

I am a beginner in Cocos2d and I wanted to display coin sprites as soon as it moves off the screen with a 5 second delay. So this is what I wrote in my main gameplay layer to add 7 coins in a row:

- (void)coinSidewaysRowOne { 
    if (coinSide1 == FALSE)
    {
        coinSide1 = TRUE;
        NSLog(@"coinSide1 = TRUE");
        int originalX = 500;
        for(int i = 0; i < 8; i++)
        {
            CCSprite *coinHorizontal = [CCSprite spriteWithFile:@"bubble.png"];
            coinHorizontal.position = ccp(originalX, 150);
            originalX += 20;

            [self addChild:coinHorizontal];
            [coinArray addObject:coinHorizontal];
        }
    }
}

And then, in my updateRunning method I added this, so when the coins spawn outside the screen, they move to the left and disappear:

// Move coins off the screen and make them move away
    for (CCSprite *coin in coinArray) {
        // apply background scroll speed
        float backgroundScrollSpeedX = [[GameMechanics sharedGameMechanics] backGroundScrollSpeedX];
        float xSpeed = 1.09 * backgroundScrollSpeedX;

        // move the coin until it leaves the left edge of the screen
        if (coin.position.x > (coin.contentSize.width * (-1)))
        {
            coin.position = ccp(coin.position.x - (xSpeed*delta), coin.position.y);
        }
    }

So right now, when I run this, the coins move in from the right and move off the screen from the left. How do I make it so that when the coins move to the left and go off the screen, have a five second delay and then have new coins come back to the screen from the right like it originally did.

Thank you!

1

There are 1 best solutions below

7
Renaissance On BEST ANSWER

You can call one function which will add that sprite back the screen with delay of five seconds.

You have to add some code as follows:

for (CCSprite *coin in coinArray)
{
    // apply background scroll speed
    float backgroundScrollSpeedX = [[GameMechanics sharedGameMechanics] backGroundScrollSpeedX];
    float xSpeed = 1.09 * backgroundScrollSpeedX;

    // move the coin until it leaves the left edge of the screen
    if (coin.position.x > (coin.contentSize.width * (-1)))
    {
        coin.position = ccp(coin.position.x - (xSpeed*delta), coin.position.y);
    }
    else
    {
        [self performSelector:@selector(showSpriteAgain:) withObject:coin afterdelay:5.0f];
    }
}

And make one function which will add that sprite to screen again:

-(void) showSpriteAgain:(CCSprite *)coin 
{
coin.position = ccp(coin.position.x+screenSize.width,coin.position.y);
}

I think this is what you are looking for.