`
bit6211
  • 浏览: 73223 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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实战:JS卷——Cocos2d-JS开发

    资源名称:Cocos2d-x实战:JS卷——Cocos2d-JS开发内容简介:本书是介绍Cocos2d-x游戏编程和开发技术书籍,介绍了使用Cocos2d-JS中核心类、瓦片地图、物理引擎、音乐音效、数据持久化、网络通信、性能优化、多平台...

    Cocos2d-x游戏编程——C++篇 .iso

    Cocos2d-x游戏编程——C++篇(电子工业出版社,徐飞 著)书本配套的光盘代码,

    迷失航线-Cocos2d-x项目实战-射击类游戏-关东升

    Cocos2d-x项目实战-射击类游戏-迷失航线,版本是Cocos2d-x-3.2

    Cocos2d-x学习笔记——完全掌握C++ API与游戏项目开发.zip

    Cocos2d-x学习笔记——完全掌握C++ API与游戏项目开发.zip

    大富翁手机游戏开发实战基于Cocos2d-x3.2引擎

    本书根据大富翁项目一一展开讲解游戏开发过程中涉及的各方面内容,读者可以通过这个游戏的开发,全面掌握Cocos2d-x游戏开发的方法和技巧。 本书理论和实践相结合, 资源太大,传百度网盘了,链接在附件中,有需要的...

    cocos2d-x事件类

    在使用cocos2d-x开发游戏的过程中,为了实现逻辑和显示相分离。 在下通宵了一个晚上,写出了该事件类。 谨记,该事件只能用于cocos2d-x中。 事件发送者需要继承EventDispatcher类 事件接收者需要继承EventHandle类...

    cocos2d-x-2.1.5

    cocos2d-x-2.1.5

    Cocos2D-X游戏开发技术精解.pdf

    《Cocos2D-X游戏开发技术精解》详细介绍如何使用Cocos2D-X引擎开发自己的移动平台游戏。全书共15章,主要内容包括:Cocos2D-X引擎简介;如何建立跨平台的开发环境;引擎的核心模块——渲染框架;如何实现动态画面和...

    Cocos2d-x高级开发教程

    Cocos2d-x是移动跨平台开发最流行的游戏引擎,而本书是一本很全面的、比较‘接地气’的游戏开发教程。书中汇聚了热门手机游戏《捕鱼达人》开发的实战经验,作者从最基础的内容开始,逐步深入地介绍了Cocos2d-x的相关...

    Cocos2D-X游戏开发技术精解

    资源名称:Cocos2D-X游戏开发技术精解内容简介:Cocos2D-X是一款支持多平台的 2D手机游戏引擎,支持iOS、Android、BlackBerry等众多平台。当前,很多移动平台流行的游戏,都是基于Cocos2D-X开发的。 《Cocos2D-X...

    Cocos2d-x-3.x游戏开发之旅

    Cocos2d-x-3.x游戏开发之旅-钟迪龙著 全新pdf版和附书代码(代码为工程文件,可复制) 附带目录标签

    Cocos2d-x学习笔记——完全掌握JSAPI与游戏项目开发

    资源名称:Cocos2d-x学习笔记——完全掌握JS API与游戏项目开发资源截图: 资源太大,传百度网盘了,链接在附件中,有需要的同学自取。

    Cocos2d-x学习笔记(三)—— 坐标系

    Cocos2d-x学习笔记(三)—— 坐标系

    精通COCOS2D-X游戏开发 基础卷_2016.4-P399-13961841.pdf

    精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发 精通COCOS2D-X游戏开发

    源代码——Cocos2d-x高级开发教程

    源代码——Cocos2d-x高级开发教程源代码——Cocos2d-x高级开发教程源代码——Cocos2d-x高级开发教程源代码——Cocos2d-x高级开发教程

    cocos2d-x实战项目

    cocos2d-x实战项目 01.cocos2d-x原理及环境配置.rar 03.cocostudio使用方法及UI控制.rar 04.XML文件读取与骨骼动画.rarcocos2d-x实战项目 01.cocos2d-x原理及环境配置.rar 03.cocostudio使用方法及UI控制.rar 04.XML...

    Cocos2d-x 3.x游戏开发实战pdf含目录

    Cocos2d-x 3.x游戏开发实战pdf含目录,内容详细,强烈推荐给大家。

    Cocos2d-x学习笔记

    自己记录的Cocos2d-x学习笔记,希望能够帮助新手,快速入门,掌握cocos2d-x的开发

    cocos2d-x-3.2旧版引擎下载

    cocos2d-x-3.2下载,不多说。或者可以下载另一个资源 cocos引擎老版本集合(cocos2d-x-2.2.1 - 3.5) http://download.csdn.net/download/crazymagicdc/9982656

    Cocos2d-x 3.x游戏开发之旅

    本书是《Cocos2d-x 游戏开发之旅》的升级版,修改了2.0版进阶到3.0版后的一些内容,新增了对CocoStudio、UI编辑器、Cocos2d-x 3.0新特性以及网络方面的知识点。主要介绍常用的API使用方式;介绍如何通过官方Demo获取...

Global site tag (gtag.js) - Google Analytics