我的Cocos2d-x学习笔记(二)AppDelegate补充介绍
2015-05-26 11:17
393 查看
上一篇中只介绍AppDelegate中applicationDidFinishLaunching()函数,这个函数是程序运行的关键,在CCApplicationProtocol中声明纯虚函数,在CCApplication中进行调用。在CCApplicationProtocol中与applicationDidFinishLaunching()类似的纯虚函数还有两个,分别是:applicationDidEnterBackground()和applicationWillEnterForeground(),在CCApplicationProtocol中声明如下:
再来看看AppDelegate.cpp中的内容,代码如下:
AppDelegate的构造函数与析构函数是空的,构造函数运行时候通过执行父类构造函数初始化CCApplication单例对象,之后没有其他功能。
CCDirector* pDirector = CCDirector::sharedDirector();得到了一个CCDirector单例对象并把指针赋值给pDirector 。
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();Cocos2d-x对OpenGL进行了封装,Cocos2d-x对OpenGL进行了初始化。
pDirector->setOpenGLView(pEGLView); 设置了场景的窗口。
pDirector->setDisplayStats(true);启用FPS显示,设置是否显示游戏的帧数等调试信息。
pDirector->setAnimationInterval(1.0 / 60);设置游戏的帧率,即每秒刷新画面次数,这里是60帧每秒。
其中重点关注以下两句:
上面的第一句:CCScene *pScene = HelloWorld::scene();创建了一个场景,这个场景就是HelloWorld,然后把这个场景指针赋值给pScene,之后通过pDirector->runWithScene(pScene);把这个HelloWorld场景运行起来。我们需要知道的是pDirector是一个CCDirector单例对象的指针,CCDirector是控制游戏中各种元素的类。
总结一下:CCApplicationProtocol中定义了运行时用到的几个接口,前台运行、进入后台运行、返回前台运行三个接口;CCApplication中包装了跨平台的实现;AppDelegate
则具体实现了CCApplicationProtocol定义的各个接口。
class CC_DLL CCApplicationProtocol { public: /** @brief Implement CCDirector and CCScene init code here. @return true Initialize success, app continue. @return false Initialize failed, app terminate. */ virtual bool applicationDidFinishLaunching() = 0; /** @brief The function be called when the application enter background @param the pointer of the application */ virtual void applicationDidEnterBackground() = 0; /** @brief The function be called when the application enter foreground @param the pointer of the application */ virtual void applicationWillEnterForeground() = 0; }在AppDelegate对其中类成员函数进行覆写,AppDelegate.h中的代码如下:
#ifndef _APP_DELEGATE_H_ #define _APP_DELEGATE_H_ #include "cocos2d.h" /** @brief The cocos2d Application. The reason for implement as private inheritance is to hide some interface call by CCDirector. */ class AppDelegate : private cocos2d::CCApplication { public: AppDelegate(); virtual ~AppDelegate(); /** @brief Implement CCDirector and CCScene init code here. @return true Initialize success, app continue. @return false Initialize failed, app terminate. */ virtual bool applicationDidFinishLaunching(); /** @brief The function be called when the application enter background @param the pointer of the application */ virtual void applicationDidEnterBackground(); /** @brief The function be called when the application enter foreground @param the pointer of the application */ virtual void applicationWillEnterForeground(); }; #endif // _APP_DELEGATE_H_由官方的注释就可知道applicationDidEnterBackground()和applicationWillEnterForeground()的作用,applicationDidEnterBackground()函数是游戏程序进入后台运行时候调用的,applicationWillEnterForeground()函数是游戏程序从后台恢复到前台运行时候调用的。
再来看看AppDelegate.cpp中的内容,代码如下:
#include "AppDelegate.h" USING_NS_CC; AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate() { } bool AppDelegate::applicationDidFinishLaunching() { // initialize director CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); // turn on display FPS pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60); CCScene *pScene = HelloWorld::scene(); // run pDirector->runWithScene(pScene); 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() { CCDirector::sharedDirector()->stopAnimation(); // if you use SimpleAudioEngine, it must be pause // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); } // this function will be called when the app is active again void AppDelegate::applicationWillEnterForeground() { CCDirector::sharedDirector()->startAnimation(); // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); }
AppDelegate的构造函数与析构函数是空的,构造函数运行时候通过执行父类构造函数初始化CCApplication单例对象,之后没有其他功能。
CCDirector* pDirector = CCDirector::sharedDirector();得到了一个CCDirector单例对象并把指针赋值给pDirector 。
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();Cocos2d-x对OpenGL进行了封装,Cocos2d-x对OpenGL进行了初始化。
pDirector->setOpenGLView(pEGLView); 设置了场景的窗口。
pDirector->setDisplayStats(true);启用FPS显示,设置是否显示游戏的帧数等调试信息。
pDirector->setAnimationInterval(1.0 / 60);设置游戏的帧率,即每秒刷新画面次数,这里是60帧每秒。
其中重点关注以下两句:
CCScene *pScene = HelloWorld::scene(); // run pDirector->runWithScene(pScene);
上面的第一句:CCScene *pScene = HelloWorld::scene();创建了一个场景,这个场景就是HelloWorld,然后把这个场景指针赋值给pScene,之后通过pDirector->runWithScene(pScene);把这个HelloWorld场景运行起来。我们需要知道的是pDirector是一个CCDirector单例对象的指针,CCDirector是控制游戏中各种元素的类。
总结一下:CCApplicationProtocol中定义了运行时用到的几个接口,前台运行、进入后台运行、返回前台运行三个接口;CCApplication中包装了跨平台的实现;AppDelegate
则具体实现了CCApplicationProtocol定义的各个接口。
相关文章推荐
- 我的Cocos2d-x学习笔记(二)AppDelegate补充介绍
- cocos2d学习之CCAppDelegate
- cocos2d-x学习笔记【二】导演、场景、层。精灵介绍
- 我的Cocos2d-x学习笔记(二十二)CCTextFieldTTF (文字输入)、CCTextFieldDelegate(输入通知事件)
- cocos2d-x学习笔记10:动作3:补间动作(补充修饰中间阶段)
- iOS学习 应用程序对象介绍AppDelegate 信息提醒
- 【吼吼睡cocos2d学习笔记】第一章 - 介绍cocos2d
- cocos2d-x入门学习笔记,主要介绍cocos2d-x的基本结构,并且介绍引擎自带的示例
- Cocoa学习笔记 Cocos2d 各种动作介绍 (下)
- [iCloud]iCloud学习笔记--APP内启用iCloud及CloudKit Dashboard介绍
- IOS学习笔记21 App介绍页面制作
- cocos 2d test-cpp 学习笔记之AppDelegate.cpp 代码分析
- (原)cocos2d笔记——解析HelloWorldAppDelegate
- cocos2d 学习笔记 - 【转载】cocos2dx 不要直接在 onEnter 里面 addTargetedDelegate (或 addStandardDelegate)
- cocos2d-x学习笔记之CCLayer、CCLayerColor、CCLayerGradient、CCLayerMultiplex场景层介绍
- 【吼吼睡cocos2d学习笔记】第一章 - 介绍cocos2d
- iOS学习笔记(2)AppDelegate的生命周期.
- Cocoa学习笔记 Cocos2d 各种动作介绍 (下)
- Cocoa学习笔记 Cocos2d 各种动作介绍 (上)
- 转-cocos2d-x 之 AppDelegate学习