我目前正在为iOS编写Cocos2d游戏。
在这个游戏中,我创建了一个GameState单身人士,以保存我的游戏状态(项目细节和位置,持续时间,分数等)。
我的主要CCScene包含一个-(void) saveData:
方法,当从正在运行的游戏中调用时(玩家点击backToMenu按钮 - > -(void) backToMenu:
),执行相应的操作:
我们被送回菜单,因为GameState.sharedState -> PLAYING = true
,会出现一个简历按钮,让我们可以恢复当前的游戏。
直到这里,预计会有效。
现在,我如何从-(void) backToMenu:
的方法appController
调用方法applicationWillEnterBackground
?
我尝试调用[[CCDirector sharedDirector] runningScene]
但它在某些方面崩溃了,这也意味着我甚至不确定我保存了适当的内容。
提前感谢您的帮助。
答案 0 :(得分:1)
在AppDelegate.h中添加:
@class CCLayer;
@interface AppController : NSObject <UIApplicationDelegate, CCDirectorDelegate,UIGestureRecognizerDelegate>
{
CCLayer *mCurrentLayer;
}
@property (nonatomic, retain) CCLayer *currentLayer;
在AppDelegate.mm中添加:
@implementation AppController
@synthesize currentLayer = mCurrentLayer;
在你的Layer init类中使用它。在所有场景方法中。
@implementation MyMainMenu
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
MyMainMenu *layer = [MyMainMenu node];
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
app.currentLayer = layer;
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
你可以在项目的任何地方查看..
在appDelegate
中-(void) applicationDidEnterBackground:(UIApplication*)application
{
if([self.currentLayer isKindOfClass:[MyMainMenu class]])
MyMainMenu *mm = (MyMainMenu*) self.currentLayer;
[mm calFunction];
}
在其他课程中:
-(void)callUpdateButtonsInLayer
{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
if([app.currentLayer isKindOfClass:[MyMainMenu class]])
{
MyMainMenu *mm = (MyMainMenu*) app.currentLayer;
[mm calFunction];
}
}
答案 1 :(得分:0)
更新:刚刚找到关于this的信息。
例如,我可以在appController的NSNotification
方法中创建applicationWillEnterBackground
,然后在我的CCLayer对象中处理它,我在其init
方法中添加NSObserver以接收此类通知?