应用程序都有生命周期管理。比如Android应用程序的OnStart, OnStop, OnCreate等周期方法。同样,Cocos2D-X 也有它的生命周期。
下面通过代码分析下它的生命周期。
定义一个类 LifeCircleLogger.
// LifeCircleLogger.h
using namespace std;
class LifeCircleLogger
{
string m_msg;
public:
LifeCircleLogger();
~LifeCircleLogger();
LifeCircleLogger(const string& msg);
};
#define LOG_FUNCTION_LIFE LifeCircleLogger(__FUNCTION__)
// LifeCircleLogger.cpp
#include "LifeCircleLogger.h"
LifeCircleLogger::LifeCircleLogger()
{
}
LifeCircleLogger::LifeCircleLogger(const string&
msg): m_msg(msg)
{
cocos2d::CCLog( "%s BEGINS!" ,
m_msg.c_str());
}
LifeCircleLogger::~LifeCircleLogger()
{
cocos2d::CCLog( "%s ENDS!" ,
m_msg.c_str());
}
然后进入AppDelegate.cpp, 在生命周期方法:
AppDelegate::AppDelegate()
AppDelegate::~AppDelegate()
bool AppDelegate::applicationDidFinishLaunching()
void AppDelegate::applicationDidEnterBackground()
void AppDelegate::applicationWillEnterForeground()
添加宏 LOG_FUNCTION_LIFE
运行程序后我们就可以在控制台输出中得到Cocos2d-x的生命周期运行原理了。[ 如上图所示 ]
OK, 这里的生命周期分析对于其他应用也是同样有效的,算一个小工具把。