cocos2d-iphoneccaction

CCActionDelay in End Game Sequence


My hero sprite is on a physics sprite. If the player (user) doesn't move the hero for 2 seconds while touching the sprite I want to end the game. In the update method I am checking to see if the two sprites are touching for longer than 2 seconds and if true run "game over" action. This is the code

if( TWO SPRITES ARE TOUCHING && USER ISN"T TOUCHING THE SCREEN ){
    [_hero runAction:[CCActionSequence actions:[CCActionDelay actionWithDuration:2.0f],
                      _gameOverAction,
                      nil]];

}

The end game action runs even if the user is holding down. Basically if you don't touch the screen to move the sprite the game should end. Any suggestions?


Solution

  • In your current code as soon as two sprites are touching and the user isn't touching the screen it will quick off the action. It will happen in 2 seconds no matter what as it is never cancelled.

    What you need to do instead is keep a variable of the time it last happened and check to see if 2 seconds have passed since then in your update loop. If it has then you can call the game over method.