您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x 给函数接口生命周期添加检测函数,用于调试程序时检测错误。

2013-08-23 12:20 609 查看
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!!!

在离开函数以后(return true; 之后)
打印

applicationDidFinishLaunching END!!!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐