2
\$\begingroup\$

I am a beginner in cocos2d and I wanted to display coin CCSprites 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);
        }
    }

Update: What I've tried doing is adding this below the if statement above:

else {
        coin.visible = TRUE;
        coin.position = ccp(500, 150);
     }

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.

\$\endgroup\$
2
  • \$\begingroup\$ What have you tried already? Do you know how to add a delay? Just write out the steps you want to do, then look up how to do each of the steps. \$\endgroup\$
    – House
    Jul 19, 2013 at 4:28
  • \$\begingroup\$ Check my update for what I've tried \$\endgroup\$ Jul 19, 2013 at 4:59

1 Answer 1

1
\$\begingroup\$

I just put my logic.

You have updateRunning method, in this method you have a logic of move coin at right to left.

Here you need to use CCMoveTo Action,

// move the coin until it leaves the left edge of the screen
if (coin.position.x > (coin.contentSize.width * (-1)))
{
   [coin runAction:
     [CCSequence actions: // Here set MoveDuration as you need , may be "5";
      [CCMoveTo actionWithDuration:MoveDuration position:ccp(coin.position.x - (xSpeed*delta), coin.position.y)],
      [CCCallBlockN actionWithBlock:^(CCNode *node) {
         // Here do stuff when your action is performed.
     }],
      nil]];
}

And also call your updateRunning method with specific time by using NSTimer such like, call updateRunning

[NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(updateRunning) userInfo:nil repeats:YES]; /// Set Time interval as you need; 
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .