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

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!!!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值