gpt4 book ai didi

ios - 恢复游戏 cocos2d

转载 作者:行者123 更新时间:2023-12-01 19:26:29 25 4
gpt4 key购买 nike

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

暂停:

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

谢谢

最佳答案

您应该向您的应用程序委托(delegate)添加一个属性,该属性将跟踪暂停是由用户点击暂停按钮还是自动引起的。

在 YourAppDelegate.h 中:

@property (nonatomic) BOOL userPaused;

在 YourAppDelegate.h 中:
@synthesize userPaused;

然后在场景的暂停方法中,添加:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.userPaused = YES;

在场景的 resume 方法中,添加:
YourAppDelegate *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.userPaused = NO;

现在您可以编辑您的应用程序委托(delegate)的 -applicationWillResignActive: 方法以仅在 userPaused 未设置为 YES 时恢复。
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (!self.userPaused) {
[[CCDirector sharedDirector] resume];
}
}

关于ios - 恢复游戏 cocos2d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7214939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com