Resume game cocos2d
I used he code below to handle pause and resume buttons in my game
To Pause:
-(void)pauseTapped{
...
[[SimpleAudioEngine sharedEngine] pauseBackgroundMusic];
[[CCDirector sharedDirector] pause];
...
}
To Resume:
-(void)resumeGame: (id)sender {
...
[self removeChild:pauseMenu 开发者_运维百科cleanup:YES];
[[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];
[[CCDirector sharedDirector] resume];
...
}
The problem is if the used click pause then enterBackground (click Home button) mode when he returns, the game automatically resume and the pause menu still exist
Any idea will be great
UPDATE:
the AppDelegate code:
- (void)applicationWillResignActive:(UIApplication *)application {
[[CCDirector sharedDirector] pause];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[CCDirector sharedDirector] resume];
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
[[CCDirector sharedDirector] purgeCachedData];
}
-(void) applicationDidEnterBackground:(UIApplication*)application {
[[CCDirector sharedDirector] stopAnimation];
}
-(void) applicationWillEnterForeground:(UIApplication*)application {
[[CCDirector sharedDirector] startAnimation];
}
- (void)applicationWillTerminate:(UIApplication *)application {
CCDirector *director = [CCDirector sharedDirector];
[[director openGLView] removeFromSuperview];
[viewController release];
[window release];
[director end];
}
- (void)applicationSignificantTimeChange:(UIApplication *)application {
[[CCDirector sharedDirector] setNextDeltaTimeZero:YES];
}
- (void)dealloc {
[[CCDirector sharedDirector] end];
[window release];
[super dealloc];
}
Thank you
You should add a property to your app delegate that will keep track if pause was caused by user tapping pause button or automatically.
Inside YourAppDelegate.h:
@property (nonatomic) BOOL userPaused;
And inside YourAppDelegate.h:
@synthesize userPaused;
Then inside your scene's pause method, add:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.userPaused = YES;
And inside your scene's resume method, add:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.userPaused = NO;
Now you can edit your app delegate's -applicationWillResignActive: method to only resume if userPaused is not set to YES.
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (!self.userPaused) {
[[CCDirector sharedDirector] resume];
}
}
精彩评论