I know this question has been asked and answered before but none of the answers seem to fit what I am trying to do. I need to be able to pause animations but still be able to have my pause menu come up and ask the user for input. Is this possible and if so how do I go about it.
9 Answers
There are 2 methods pause and resume for this purpose in CCDirector:
[[CCDirector sharedDirector] pause]; and
[[CCDirector sharedDirector] resume]; From the documentation:
The running scene will be drawed but all scheduled timers will be paused While paused, the draw rate will be 4 FPS to reduce CPU consumption
Comments
I suggest you to push the "pause scene" on the cocos2d scene stack.
CCScene * pauseScene = ...; [[CCDirector sharedDirector] pushScene:pauseScene]; This will automatically pause your current scene (which I assume is the level scene) and will run the "pause scene" so that user can interact with it.
It is very important to remember that after the instructions above have been executed, you will have 2 scenes in the scenes stack of cocos2d (the level scene [frozen] + the pause scene [running]).
So, once you are in the "pause scene", if you want resume the level you just need to pop the pause scene from the stack.
[[CCDirector sharedDirector] popScene]; This will automatically remove the "pause scene" and resume the previous scene in the stack (which is the level scene).
Please remember that you will always need to "pop" the pause scene sometime. It is important not to forget the "level scene" is still in the scene stack of cocos2d!
E.g.: if in the "Pause Scene" you have a button to load the level selection menu and permanently leave the current level, you should implement it as follow:
CCScene * levelSelector = ... [[CCDirector sharedDirector] popScene]; // to remove the "pause scene" from the stack [[CCDirector sharedDirector] replaceScene:levelSelector]; // replace the "level scene" with the "levelSelector scene" Comments
I tried all of the suggestions but none of them worked in my case, so this is what I ended up doing which worked really well for me. This is to pause.
[self pauseSchedulerAndActions]; for (CCNode *child in [self children]) { [child pauseSchedulerAndActions]; } And this to resume:
[selfReference resumeSchedulerAndActions]; for (CCNode *child in [selfReference children]) { [child resumeSchedulerAndActions]; } Comments
With Cocos 2D Version 2 the action manager is no more a singleton. But rather the code has been fused with the CCDirector.
So this is the way to pause all actions in a particular scene and resume them as well.
-(void)pauseGamePlayScene{ for(CCSprite *sprite in [self children]) { [[[CCDirector sharedDirector] actionManager]pauseTarget:sprite]; } } -(void)resumeGamePlayScene{ for(CCSprite *sprite in [self children]) { [[[CCDirector sharedDirector] actionManager]resumeTarget:sprite]; } } Cocos 2d Ver 2 Action Manager Documentation
Hope this helps.
Comments
Stopping just animations?
[ [ CCDirector sharedDirector ] stopAnimation ]; [ [ CCDirector sharedDirector ] startAnimation ]; If you want to stop everything, pause as you have seen done everywhere else (after the menu has been moved into place), then unpause once the user touches a certain thing.
Comments
I am new to cocos2D and have used seperate game-loop (rather than scheduleupdate) and am unscheduling it along with some more schedulers. It would automatically pause the gameplay at the same time i am placing a new layer (i.e. Pauselayer) ontop of the gamelayer. Appears pretty complex, but worked for me.
Comments
I use:
@interface CCNode (PauseAdded) @end @implementation CCNode (PauseAdded) -( void ) traversePause { CCNode* a = [children_ lastObject]; CCARRAY_FOREACH( children_, a ) { [scheduler_ pauseTarget:a]; [actionManager_ pauseTarget:a]; [a traversePause]; } [scheduler_ pauseTarget:self]; [actionManager_ pauseTarget:self]; } -( void ) traverseResume { CCNode* a = [children_ lastObject]; CCARRAY_FOREACH( children_, a ) { [a traverseResume]; [scheduler_ resumeTarget:a]; [actionManager_ resumeTarget:a]; } [scheduler_ resumeTarget:self]; [actionManager_ resumeTarget:self]; } @end That way you can just do something like this assuming your game layer is separated from the pause layer:
[scene.game_layer traversePause]; And to unpause:
[scene.game_layer traverseResume]; Comments
Try this PauseButton class https://github.com/iabtyagi/custom-pause-cocos2d . does exactly what you are looking for.