★ TouchArcade needs your help. Click here to support us on Patreon.

Best way to pause for half a second with cocos2d

09-24-2009, 08:14 PM
#1
Joined: Aug 2009
Location: San Francisco
Posts: 362
Send a message via Skype™ to micah
Best way to pause for half a second with cocos2d

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?

--=] Insurgent Games website | twitter [=-
Cryptose (TA) - Skeleton Key (TA) - Skeleton Key HD (TA link) - Aeropack (TA)
09-24-2009, 08:23 PM
#2
Joined: May 2009
Location: Palmerston North, New Zealand
Posts: 464
Quote:
Originally Posted by micah View Post
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?
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.

Check out Sheepish, 100+ levels of puzzle fun! With sheep!
And more about us: www.pixelthismobile.com
plus+ id: Newtz
09-24-2009, 08:42 PM
#3
Joined: Aug 2009
Location: San Francisco
Posts: 362
Send a message via Skype™ to micah
Thanks, this worked great.

--=] Insurgent Games website | twitter [=-
Cryptose (TA) - Skeleton Key (TA) - Skeleton Key HD (TA link) - Aeropack (TA)
09-24-2009, 08:52 PM
#4
Joined: May 2009
Location: Palmerston North, New Zealand
Posts: 464
Quote:
Originally Posted by micah View Post
Thanks, this worked great.
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];

Check out Sheepish, 100+ levels of puzzle fun! With sheep!
And more about us: www.pixelthismobile.com
plus+ id: Newtz
09-24-2009, 10:20 PM
#5
Joined: Mar 2009
Location: Boston, mass
Posts: 39
Quote:
Originally Posted by PixelthisMike View Post
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.
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.

__________________________________
Splat; shoot stuff and make art, what more could you ask for? Currently sporting a 4.78 rating in the app store!