恢复游戏cocos2d

时间:2011-08-27 13:45:44

标签: ios multithreading cocos2d-iphone

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

暂停:

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

恢复:

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

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

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

任何想法都会很棒

更新:

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

谢谢

1 个答案:

答案 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];
    }
}