博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
我的Cocos2d-x学习笔记(二)AppDelegate补充介绍
阅读量:4707 次
发布时间:2019-06-10

本文共 4188 字,大约阅读时间需要 13 分钟。

    上一篇中只介绍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定义的各个接口。

转载于:https://www.cnblogs.com/gongyan/p/4539411.html

你可能感兴趣的文章
解决在vue中,自用mask模态框出来后,下层的元素依旧可以滑动的问题
查看>>
PAT甲级题解-1123. Is It a Complete AVL Tree (30)-AVL树+满二叉树
查看>>
项目开发总结报告(GB8567——88)
查看>>
SSH加固
查看>>
iOS IM开发的一些开源、框架和教程等资料
查看>>
python 二维字典
查看>>
实验吧之【天下武功唯快不破】
查看>>
2019-3-25多线程的同步与互斥(互斥锁、条件变量、读写锁、自旋锁、信号量)...
查看>>
win7-64 mysql的安装
查看>>
dcm4chee 修改默认(0002,0013) ImplementationVersionName
查看>>
maven3在eclipse3.4.2中创建java web项目
查看>>
发布时间 sql语句
查看>>
黑马程序员 ExecuteReader执行查询
查看>>
记一些从数学和程序设计中体会到的思想
查看>>
题目1462:两船载物问题
查看>>
POJ 2378 Tree Cutting(树形DP,水)
查看>>
UVA 116 Unidirectional TSP (白书dp)
查看>>
第三方测速工具
查看>>
数据访问 投票习题
查看>>
cnblog!i'm coming!
查看>>