Ok so I know that cocos "pauses" the director by default when it leaves the game. and all. Perfect. BUT I have a pause menu that I show when pausing the game and I'd like to show that right when the applicationWillResignActive. I have it as a nice method in my GameScene and like when they hit the pause button I call it up and it shows a Resume button as well as pause the game. I want that to show when the game closes/get interrupted so the user isn't immediately in the game when they come back but instead they are seeing the resume button and can press it when they are ready.
Auto Pause when leaving Game
(7 posts) (3 voices)-
Posted 1 year ago #
-
I guess I can answer this question, maybe I did not understand it but... Let's try.
You can use pushScene and popScene. (look for it in the documation).
In the method "applicationWillEnterBackGround" ou have "stopAnimation" and in the method "applicationDidEnterForeground" let "startAnimation" and you add "[[CCDirector SharedDirector] popScene:[your pause scene]]; .Have a look at the documentation.
Posted 1 year ago # -
Well I don't have a pause scene. I have a pause menu that simply shows overtop of my game and in the pause method I basically have addChild pauseMenu and Director pause. All I want to do is pull that method when the application entersBackground/Resigns/etc.
But one problem is making it a class method instead of instance makes all the addChild's not work. And another problem is that since I have a GameScene and a HomeScene. There is no need to call the pause method when on the HomeScene. So what I want is something like:
If (CurrentScene==GameScene) {
[GameScene pauseTheGame]
} else {
[[CCDirector sharedDirector] pause];
}But using runningScene and isRunning and all isn't working at all because Cocos doesnt return any useful information as far as which scene is running. instead it shoulds a bunch of information like thread numbers and tags.
Posted 1 year ago # -
Hi EmperiorEric,
To test which class is currently running from the AppDelegate use the following code;
if ([[[CCDirector sharedDirector] runningScene] isKindOfClass:[MenuScene class]]) { NSLog(@"MenuScene"); }THis can go in your applicationDidEnterBackground method, I believe you will have to call the CCDirectors pause command regardless of which scene is running, so no need for the else statement.
Regards,
TazWeb: http://www.silverbacktechsol.co.uk/
Twitter: @stssupport
Latest App: ChainedPosted 1 year ago # -
That is exactly the line of code I needed. That code is definitely doing what I wanted cause now I know which scene is actually running and can respond accordingly. Still have some glitches. This is the code i have now and what you gave me works. But now when I come back to the game the pause menu isn't there the app starts like it wasn't running. any ideas where I screwed up?
if ([[[CCDirector sharedDirector] runningScene] isKindOfClass:[GameScene class]]) {
[GameScene pauseTheGame];
[[CCDirector sharedDirector] pause];
} else {
[[CCDirector sharedDirector] pause];
}Oh and here is the pauseTheGame method if it helps
-(void)pauseTheGame {
CCNode *pauseTest = [self getChildByTag:9080];
pauseTest.visible = NO;// PAUSE
[[CCDirector sharedDirector] pause];// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];// Show Pause Menu
CCLabelTTF *copyLabel = [CCLabelTTF labelWithString:@"© PopArcade" fontName:@"Marker Felt" fontSize:12];
copyLabel.position = ccp( size.width /2 , 7 );CCMenuItemFont *resume = [CCMenuItemFont itemFromString:@"Resume" target:self selector:@selector(resumeTheGame)];
CCMenuItemFont *quit = [CCMenuItemFont itemFromString:@"Quit" target:self selector:@selector(quitTheGame)];
CCMenuItemFont *options = [CCMenuItemFont itemFromString:@"Options" target:self selector:@selector(showOptions)];CCMenu *menu = [CCMenu menuWithItems:resume, quit, options, nil];
menu.position = CGPointZero;resume.position = ccp(size.width /2 ,300);
quit.position = ccp(size.width /2 ,250);
options.position = ccp(size.width /2 ,200);[self addChild:menu z:2 tag:5678];
[self addChild:copyLabel z:2 tag:5679];
}Posted 1 year ago # -
Thanks for everyone's help. I have finally gotten something that works figured out! It is a combination of using
if ([[[CCDirector sharedDirector] runningScene] isKindOfClass:[MenuScene class]]) {
NSLog(@"MenuScene");
}In the delegate and poping/pushing scenes for the actual pausing. Seems to be working everything I wanted. Thanks very much everyone!
Posted 1 year ago # -
Figured I would post more details on what I did to solve this issue incase someone else needs the help!
So here is my Delegate code:
- (void)applicationWillResignActive:(UIApplication *)application {
if ([[[CCDirector sharedDirector] runningScene] isKindOfClass:[GameScene class]]) {
[[GameScene sharedGameScene] pauseTheGame];
} else {
[[CCDirector sharedDirector] pause];
}
}I've created a GameScene director so I can easily call all my methods. And if the game is running I call pauseTheGame, very simple. The new pauseTheGame method is this:
-(void)pauseTheGame {
[[CCDirector sharedDirector] pushScene:[PauseScene scene]];
[[CCDirector sharedDirector] pause];
}Very simple, pushes the scene like @Cohars said to do. And my pause scene is simply my OLD pauseTheGame method in its own scene:
-(id) init {
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init] )) {// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];// Show Pause Menu
CCLabelTTF *copyLabel = [CCLabelTTF labelWithString:@"© PopArcade" fontName:@"Marker Felt" fontSize:12];
copyLabel.position = ccp( size.width /2 , 7 );CCMenuItemFont *resume = [CCMenuItemFont itemFromString:@"Resume" target:self selector:@selector(resumeTheGame)];
CCMenuItemFont *quit = [CCMenuItemFont itemFromString:@"Quit" target:self selector:@selector(quitTheGame)];
CCMenuItemFont *options = [CCMenuItemFont itemFromString:@"Options" target:self selector:@selector(showOptions)];CCMenu *menu = [CCMenu menuWithItems:resume, quit, options, nil];
menu.position = CGPointZero;resume.position = ccp(size.width /2 ,300);
quit.position = ccp(size.width /2 ,250);
options.position = ccp(size.width /2 ,200);[self addChild:menu z:2 tag:5678];
[self addChild:copyLabel z:2 tag:5679];NSLog(@"Game Paused");
}
return self;
}And to resume the Game I've got these two methods to either pop the GameScene or to go back to the Main Menu.
-(void)resumeTheGame {
[[CCDirector sharedDirector] popScene];
[[CCDirector sharedDirector] resume];
}-(void)quitTheGame {
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] replaceScene:[CCTransitionSlideInL transitionWithDuration:0.5f scene:[Menu node]]];
}Thanks EVERYONE for your help. This has been a great first time Cocos2d forum experience. Cheers!
Posted 1 year ago #
Reply
You must log in to post.