cocos2d-x学习备忘之三——iphone中,游戏前后台切换调用方法顺序

     在iPhone中,cocos2d-x启动和前后台切换时,调用的方法和纯iPhone框架几乎是相同的。只是中间穿插了AppDelegate.cpp中的方法。其中AppDelegate.cpp主要代码如下:

bool AppDelegate::applicationDidFinishLaunching()
{
    ...

    CCLog("i am in didfinishLaunching");

    return true;
}


// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
    ...

    CCLog("i am in enterbackground");
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
    ...
   	
    CCLog("i am in enterforeground");
}

 

而AppController.mm主要代码如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.

    // Add the view controller's view to the window and display.
    window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]];
    EAGLView *__glView = [EAGLView viewWithFrame: [window bounds]
                                     pixelFormat: kEAGLColorFormatRGBA8
                                     depthFormat: GL_DEPTH_COMPONENT16_OES
                              preserveBackbuffer: NO
                                      sharegroup: nil
                                   multiSampling: NO
                                 numberOfSamples: 0 ];
    
    // Use RootViewController manage EAGLView 
    viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil];
    viewController.wantsFullScreenLayout = YES;
    viewController.view = __glView;

    // Set RootViewController to window
    [window addSubview: viewController.view];
    [window makeKeyAndVisible];

    [[UIApplication sharedApplication] setStatusBarHidden: YES];
    
    cocos2d::CCApplication::sharedApplication().run();
	
	NSLog(@"didFinishLaunchingWithOptions");
	
	
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
	//cocos2d::CCDirector::sharedDirector()->pause();
	
	SceneManager* sceneManager = SceneManager::getInstance();
	MainScene* mainScene = (MainScene*)sceneManager->getNowScene();
	MainLayer* mainLayer = (MainLayer*)mainScene->getChildByTag(MainScene::MAINLAYERTAG);
	
	//when in the mainLayer and the game start, we set the pause when app resign active
	if (mainLayer != NULL && mainLayer->getTimeSum() > 0.1f) {
		mainLayer->pauseGame();
	}
	
	NSLog(@"applicationWillResignActive");
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
	
	//cocos2d::CCDirector::sharedDirector()->resume();
	NSLog(@"applicationDidBecomeActive");
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
     */
    cocos2d::CCApplication::sharedApplication().applicationDidEnterBackground();
	
	SceneManager* sceneManager = SceneManager::getInstance();
	MainScene* mainScene = (MainScene*)sceneManager->getNowScene();
	MainLayer* mainLayer = (MainLayer*)mainScene->getChildByTag(MainScene::MAINLAYERTAG);
	
	if (mainLayer != NULL && mainLayer->getTimeSum() > 0.1f) {
		mainLayer->saveTheLongestTime();
	}
	
	NSLog(@"applicationDidEnterBackground");
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    /*
     Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
     */
    cocos2d::CCApplication::sharedApplication().applicationWillEnterForeground();
	NSLog(@"applicationWillEnterForeground");
}

 其中,当游戏启动时,输出为:

Cocos2d: i am in didfinishLaunching
didFinishLaunchingWithOptions
applicationDidBecomeActive

 

当点击Home键,游戏进入后台时,输出为:

applicationWillResignActive
Cocos2d: i am in enterbackground
applicationDidEnterBackground

 

当游戏从后台进入前台时,输出为:

Cocos2d: i am in enterforeground
applicationWillEnterForeground
applicationDidBecomeActive

 

可见AppDelegate.cpp的代码是在穿插在AppController.mm的代码中执行的,这让我想到了Spring的AOP,但目前不知道这样的用途是什么,仅仅记录。然后当游戏前后台切换时,对游戏设置暂停、或者保存用户数据等等操作,应该在哪个方法中进行,在注释中已经写得很明白了。

你可能感兴趣的:(cocos2d-x)