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.
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,