cocos2d-x 游戏开发时,遇到崩溃往往需要自己一步一步的慢慢调试,这种方法比较费时间,我们可以采用在开发时给函数的入口处添加生命周期检测函数。这样可以从控制台打印信息看出程序出问题时所在函数。(如果是采用 "autorelease()" 处理的内存,会在当前帧执行过后才释放内存,这时候出现内存用次方法还是不能看出。。有人有更好的解决方法可以留言~~);
//
// FunctionLifeLog.h
// TouchSprite
//
// Created by LangYi on 13-8-23.
//
//
#ifndef __TouchSprite__FunctionLifeLog__
#define __TouchSprite__FunctionLifeLog__
#include "cocos2d.h"
class FunctionLifeLog:public cocos2d::CCObject
{
public:
inline FunctionLifeLog()
{
m_strMsg = cocos2d::CCString::create("");
}
inline FunctionLifeLog(const char* msg)
{
m_strMsg = cocos2d::CCString::create(msg);
cocos2d::CCLog("%s BEGIN!!!",m_strMsg->getCString());
}
inline ~FunctionLifeLog()
{
cocos2d::CCLog("%s END!!!",m_strMsg->getCString());
}
private:
cocos2d::CCString* m_strMsg;
};
#define FUNCTION_LIFE_LOG FunctionLifeLog(__FUNCTION__);
#endif /* defined(__TouchSprite__FunctionLifeLog__) */
FUNCTION_LIFE_LOG
就行。
如:
bool AppDelegate::applicationDidFinishLaunching()
{
FUNCTION_LIFE_LOG //打印函数周期
// 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
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
}
会在进入函数时 打印
applicationDidFinishLaunching BEGIN!!!
打印
applicationDidFinishLaunching END!!!