2.4 AppDelegate 的 3 个生命周期

时间:2023-05-31 21:35:14

Classed/AppDelegate.cpp 文件内容如下:

#include "cocos2d.h"
#include "CCEGLView.h"
#include "AppDelegate.h"
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h" using namespace CocosDenshion; USING_NS_CC; AppDelegate::AppDelegate()
{
} AppDelegate::~AppDelegate()
{
SimpleAudioEngine::end();
} bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director(导演初始化)
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); // turn on display FPS(设置显示状态信息:当前对象个数 每一帧使用时间 当前游戏整体运行帧数)
pDirector->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this(设置游戏帧率为每秒60帧)
pDirector->setAnimationInterval(1.0 / ); // create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene(); // run (运行 HelloWorld 场景)
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();
//设置暂停音乐
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
} // this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
//设置游戏继续
CCDirector::sharedDirector()->startAnimation();
//设置音乐继续播放
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
} /*
此类含义是当整个项目启动后,Cocos2d-x引擎会从各个平台的入口函数中接管,然后调用
AppDelegate类中的 applicationDidFinishLaunching 函数。
当程序用户切入运行后台的时候,会响应 AppDelegate 类中的 applicationDidEnterBackground 函数。
当程序用户从设备运行后台切回到当前运行程序时,会响应 AppDelegate 类中的 applicationWillEnterForeground函数。
*/