-1

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:

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);
        }
        **// This is where I am trying to make the CCSprite coin reappear** 
        else
        {
            [self performSelector:@selector(showSpriteAgain:) withObject:coin afterDelay:5.0f];
        }
    }

And then I added this method:

-(void) showSpriteAgain:(CCSprite *)coin{
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    coin.position = ccp(coin.position.x-screenSize.width,coin.position.y);
}

But the coins still don't reappear after 5 seconds. Am I doing something wrong? Thanks.

Shalin Shah
  • 8,145
  • 6
  • 31
  • 44
  • I suspect your `else` part is not getting called.. Can you put a Debug pointer, or NSLog to check.. – iphonic Jul 19 '13 at 05:58
  • I added an NSLog and as soon as the 1st wave of coins go away, it is being called, but the coins aren't re-appearing. – Shalin Shah Jul 19 '13 at 06:03
  • @ShalinShah: i think you should try something your self. I know i should not write this here but i don't like your way to post same question two times. – Renaissance Jul 19 '13 at 06:14

1 Answers1

1

Change in showSpriteAgain function:

-(void) showSpriteAgain:(CCSprite *)coin{
    CGSize screenSize = [[CCDirector sharedDirector] winSize];
    coin.position = ccp(coin.position.x + screenSize.width,coin.position.y);
}

What i did, was its moving right to left so we have to place it back to right so we have to add screennSize.width,

Renaissance
  • 564
  • 5
  • 26