1. 程式人生 > >12、Cocos2dx 3.0遊戲開發找小三之3.0中的生命周期分析

12、Cocos2dx 3.0遊戲開發找小三之3.0中的生命周期分析

ide () mil and 地理 splay ioe ase ima

重開發人員的勞動成果。轉載的時候請務必註明出處: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 則完畢了載入遊戲場景等工作。

郝萌主友情提醒: 處理好用戶的各種操作。才不會導致程序崩潰哦、、、、

12、Cocos2dx 3.0遊戲開發找小三之3.0中的生命周期分析