2011-08-27 87 views
1

我用他下面的代码来处理暂停和我的比赛恢复按钮继续游戏的cocos2d

要暂停:

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

恢复:

-(void)resumeGame: (id)sender { 
... 
    [self removeChild:pauseMenu cleanup:YES]; 

    [[SimpleAudioEngine sharedEngine] resumeBackgroundMusic]; 
    [[CCDirector sharedDirector] resume]; 
... 
} 

的问题是,如果使用点击暂停然后进入背景(点击主页按钮)模式 当他回来时,游戏会自动恢复并且暂停菜单依然存在

任何想法将是巨大的

UPDATE:

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

谢谢

+0

发布UR的appdelegate代码 –

+0

@Vjay我更新了帖子 –

+0

让我知道这是工作还是不 –

回答

3

你应该将属性添加到您的应用程序代理,将保持跟踪,如果暂停是由用户点击暂停按钮或自动引起的。

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