恢复游戏cocos2d [英] Resume game cocos2d

查看:181
本文介绍了恢复游戏cocos2d的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I used he code below to handle pause and resume buttons in my game

暂停:

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

要恢复:

-(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

任何想法都会很好

UPDATE:

AppDelegate代码:

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

谢谢

推荐答案

您应该向应用程式委托中添加一个属性,如果暂停是由用户点按暂停按钮或自动导致的,则该属性会保持跟踪。

You should add a property to your app delegate that will keep track if pause was caused by user tapping pause button or automatically.

YourAppDelegate.h:

Inside YourAppDelegate.h:

@property (nonatomic) BOOL userPaused;

并在YourAppDelegate.h内:

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;

现在,您可以编辑您的应用程序委托的-applicationWillResignActive:方法,仅当userPaused未设置为YES 。

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

这篇关于恢复游戏cocos2d的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆