Best way to pause for half a second with cocos2d

Discussion in 'Public Game Developers Forum' started by micah, Sep 25, 2009.

  1. micah

    micah Well-Known Member

    Aug 24, 2009
    362
    0
    0
    game developer
    San Francisco
    Hey all you cocos2d programmers, I'm trying to figure out how to solve a problem.

    I'm working on a game where each time you die, the game pauses for about half a second and displays a big graphic over everything, then removes the graphic and resumes.

    I've thought about using the director's pause and resume methods with a call to sleep() from unistd.h (yeah, ugly, I know), but that didn't work anyway because the screen didn't update with the death graphic before sleep() was called.

    I've thought about making a death Scene, and when you die pushing the death scene, scheduling a selector to pop it back half a second later. This works fine, but I want this graphic to be displayed over the rest of the gameplay, not on a separate scene.

    I'm still a cocos2d n00b and this is my first real game with it. Any ideas?
     
  2. PixelthisMike

    PixelthisMike Well-Known Member

    You could just use the NSObject method performSelector:withObject:afterDelay: couldn't you? For example:

    Code:
    [self performSelector:showDeathGraphic withObject:nil afterDelay:0.5];
    Assuming here that self is a Scene since it then inherits from CocosNode and therefore NSObject.
     
  3. micah

    micah Well-Known Member

    Aug 24, 2009
    362
    0
    0
    game developer
    San Francisco
    Thanks, this worked great.
     
  4. PixelthisMike

    PixelthisMike Well-Known Member

    No problem! Just realised a flaw in my code snippet so will redo for the benefit of others:

    Code:
    [self performSelector:@selector(showDeathGraphic) withObject:nil afterDelay:0.5];
    :)
     
  5. slipster216@gmail.com

    [email protected] Active Member

    Mar 11, 2009
    39
    1
    0
    Game Developer
    Boston, mass
    Thats very un-cocos way to do it. The problem with that is that your delaying for 0.5 seconds regardless of the cocos time state. In other words, if the user pauses the game through some means, that delayed call will happen regardless of this fact because an NSTimer is triggering it instead of the cocos version of time (which can be paused, warped, etc). In this case, it's an edge case and unlikely to matter, but the more proper way to do it is to use a sequence. Something like:

    Code:
    [self runAction:[[Sequence actions:[DelayTime actionWithDuration:0.5],[CallFunc actionWithTarget:self selector:@selector(myfunc)], nil]];
    
    While this is certainly an edge case, it's good to get into good habits with this kind of stuff, because the next time you do it might be game play related, and possibly be used to cheat or cause a crash.
     

Share This Page