上一篇中只介绍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;}在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 toovoID 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 againvoID 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-x学习笔记(二)AppDelegate补充介绍所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)