zoukankan      html  css  js  c++  java
  • 12、Cocos2dx 3.0游戏开发找小三之3.0中的生命周期分析

    重开发人员的劳动成果,转载的时候请务必注明出处http://blog.csdn.net/haomengzhu/article/details/27706303

    生命周期分析
    在前面文章中我们执行了第一个 Cocos2d-x 游戏,同一时候也介绍了控制游戏生命周期的 AppDelegate 文件。


    以下我们将结合一些游戏调试经常使用的技巧以及VS工具调试的方法来分析 Cocos2d-x 程序的生命周期。 

    VS工具调试
    1、查看内存窗体
    2、查看输出窗体
    3、假设程序崩溃查看调用堆栈窗体


    打开项目中的"AppDelegate.cpp"文件。

    为了清楚地理解函数间的调用关系。最好还是给每一个函数的开头加上一个日志函数调用。
    在控制台打印该函数被调用的信息。


    因此。我们建立以下的一个日志类,利用暂时变量在栈中的生命周期,搭配自己主动构造和析构函数产生的日志,
    并定义一个宏来跟踪函数名称,使用它们来打印函数的開始和结束。 

    建立一个 LifeCircleLog 类。并加入例如以下代码:
    class LifeCircleLog 
    {
     std::string m_msg;
    public:
      LifeCircleLog  (){};
      LifeCircleLog  (const string& msg):m_msg(msg)
     {
      CCLog("%s BEGINS!",m_msg.c_str());
     }
     ~ LifeCircleLog  ()
     {
      CCLog("%s ENDS!",m_msg.c_str());
     }
    };

    //CCLog:Cocos2d-x 提供的日志输出函数。
    这里出现的 CCLog 是 Cocos2d-x 的控制台输出函数。其參数方式与 C 语言的 printf 全然一致。
    用%d 表示整型。%s 表示字符串等。
    实际上,在 Windows 平台上,该函数正是通过包装 printf 函数实现的。

    在 iOS 和 Android 等平台上。这个函数有着相同的接口表示,并都能够在调试时信息打印到控制台。

    我们在 AppDelegate 类中全部函数的第一条语句前增加 LOG_FUNCTION_LIFE 宏:
    AppDelegate::AppDelegate() {
     LOG_FUNCTION_LIFE
    }
    
    AppDelegate::~AppDelegate() 
    {
     LOG_FUNCTION_LIFE
    }
    
    bool AppDelegate::applicationDidFinishLaunching() {
     LOG_FUNCTION_LIFE
    
        // initialize director
        auto director = Director::getInstance();
        auto glview = director->getOpenGLView();
        if(!glview) {
            glview = GLView::create(FontChina::G2U("郝萌主之找小三"));
            director->setOpenGLView(glview);
        }
    
        // turn on display FPS
        director->setDisplayStats(false);
    
        // set FPS. the default value is 1.0/60 if you don't call this
        director->setAnimationInterval(1.0 / 60);
    
        // create a scene. it's an autorelease object
        auto scene = HelloWorld::createScene();
    
        // run
        director->runWithScene(scene);
    
        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() {
     LOG_FUNCTION_LIFE
    
        Director::getInstance()->stopAnimation();
    
        // if you use SimpleAudioEngine, it must be pause
        SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
    }
    
    // this function will be called when the app is active again
    void AppDelegate::applicationWillEnterForeground() {
     LOG_FUNCTION_LIFE
    
        Director::getInstance()->startAnimation();
    
        // if you use SimpleAudioEngine, it must resume here
        SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
    }
    启动游戏,然后做例如以下操作:
    首先把游戏最小化,然后再把它恢复到前台,最后关闭游戏。
    完毕后回到 VS,能够在控制台中的输出窗体里看到函数调用顺序:

    AppDelegate::AppDelegate BEGINS!
    AppDelegate::AppDelegate ENDS!
    AppDelegate::applicationDidFinishLaunching BEGINS!
    AppDelegate::applicationDidFinishLaunching ENDS!

    AppDelegate::~AppDelegate BEGINS!
    AppDelegate::~AppDelegate ENDS!

    由这个实验能够看出,AppDelegate 是整个程序的入口

    实际上。AppDelegate 处理了程序生命周期中的 4 个重要事件点:
    程序完毕初始化,程序进入后台。程序重回前台和程序结束退出。
    进入后台和重回前台两个函数完毕暂停和恢复游戏的处理。
    appDidFinishingLaunching 则完毕了载入游戏场景等工作。


    郝萌主友情提醒:
    处理好用户的各种操作,才不会导致程序崩溃哦、、、、

  • 相关阅读:
    spring-boot 在启动运行脚本中执行修改表结构,执行前校验表结构是否符合,如果不符合就修改
    Ehcache flush() 源码阅读笔记
    关于超时的实现---利用Timer
    java 基础笔记--Map
    spring-boot+mybatis+ehcache实现快速查询
    笔记---html输入限制
    spring boot thymeleaf模板url上的参数传递
    xcode 8   去除无用打印信息
    项目整理 (一)
    FMDB最简单的教程-3 清空数据表并将自增字段清零
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/7109584.html
Copyright © 2011-2022 走看看