恢复游戏cocos2d

我用下面的代码来处理游戏中的暂停和恢复按钮

暂停:

-(void)pauseTapped{ ... [[SimpleAudioEngine sharedEngine] pauseBackgroundMusic]; [[CCDirector sharedDirector] pause]; ... } 

恢复:

 -(void)resumeGame: (id)sender { ... [self removeChild:pauseMenu cleanup:YES]; [[SimpleAudioEngine sharedEngine] resumeBackgroundMusic]; [[CCDirector sharedDirector] resume]; ... } 

问题是如果使用的点击暂停然后返回后返回BackBackground(单击主页按钮)模式,游戏会自动恢复,暂停菜单仍然存在

任何想法都会很棒

更新:

AppDelegate代码:

 - (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]; } 

谢谢

您应该向应用程序代理添加一个属性,该属性将跟踪是否由用户点击暂停按钮或自动导致暂停。

在YourAppDelegate.h中:

 @property (nonatomic) BOOL userPaused; 

在YourAppDelegate.h中:

 @synthesize userPaused; 

然后在场景的暂停方法中添加:

 YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.userPaused = YES; 

在场景的简历方法中,添加:

 YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; appDelegate.userPaused = NO; 

现在,您可以编辑应用委托的-applicationWillResignActive:方法,仅在userPaused未设置为YES时才恢复。

 - (void)applicationDidBecomeActive:(UIApplication *)application { if (!self.userPaused) { [[CCDirector sharedDirector] resume]; } }