上一篇中只介绍AppDelegate中applicationDidFinishLaunching()函数,这个函数是程序运行的关键,在CCApplicationProtocol中声明纯虚函数,在CCApplication中进行调用。在CCApplicationProtocol中与applicationDidFinishLaunching()类似的纯虚函数还有两个,分别是:applicationDidEnterBackground()和applicationWillEnterForeground(),在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; }@H_404_3@在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_ @H_404_3@ 由官方的注释就可知道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(); } @H_404_3@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);@H_404_3@
上面的第一句:CCScene *pScene = HelloWorld::scene();创建了一个场景,这个场景就是HelloWorld,然后把这个场景指针赋值给pScene,之后通过pDirector->runWithScene(pScene);把这个HelloWorld场景运行起来。我们需要知道的是pDirector是一个CCDirector单例对象的指针,CCDirector是控制游戏中各种元素的类。
总结一下:CCApplicationProtocol中定义了运行时用到的几个接口,前台运行、进入后台运行、返回前台运行三个接口;CCApplication中包装了跨平台的实现;AppDelegate 则具体实现了CCApplicationProtocol定义的各个接口。
原文链接:https://www.f2er.com/cocos2dx/342903.html