HelloWorld和基本4個類別介紹

上一篇我們成功執行 HelloWorld 專案 ,現在開始我們將簡單說明程式碼的架構結合4個基本類別介紹 ,OK~ 先來回想你玩過的遊戲,大部分的遊戲流程多半是

Logo流程 -> 開頭選單流程 -> 主遊戲流程 -> 遊戲結束流程 -> Logo流程(或離開)

當然~ 複雜一點的遊戲還會加上排行榜,選單音樂音效設定,轉場等等之類,在cocos2d-x中這些"流程"都視為"場景", 而場景就是 CCScene 這個類別,你可以擁有多個scene,也可以只有單個scene,不過在同一個時間只能有單個Scene運作,多個scene的切換必須依靠 CCDirector(導演) 來控制,也就是說 CCDirector(導演)根據你設定的條件來切換CCScene(場景),而在CCScene(場景)中並無法直接呈現物件,我們必須依靠CCLayer(圖層)來顯示物件,你可以想像在空無一物的場景(CCScene)中,擺上布幕或圖層(CCLayer),最後我們再把物件(CCSprite)放到圖層(CCLayer)上,所以其實玩家看到的是CCLayer和CCSprite,而CCScene並沒有任何可見性,可以聯想成CCScene在最下層然後第2層為CCLayer最後在第1層的為CCSprite, 如下圖所示














這4個基本類別的關係大致上就是如此,接下來我們一邊來看HelloWorld一邊來了解如何使用吧
請用VC++開啟上一篇建立的專案,在Solution Explorer視窗中的include資料夾為.h檔的集合,先來看看 AppDelegate.cpp ,

AppDelegate類別為所有cocos2d-x專案的先行類別,只要是cocos2d-x的專案都有這個類別,其中 applicationDidFinishLaunching()為程式的進入點,還記得剛剛提到可以擁有多個Scene嗎,你也必須指定哪個scene為第1個執行的scene

  CCDirector *pDirector = CCDirector::sharedDirector();//初始化CCDirector

初始化 pDirector 之後, 接著是一連串的設定,如

  pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());//取得OpenGL

  pDirector->setDisplayStats(true);//顯示FPS

  pDirector->setAnimationInterval(1.0 / 60);//設定FPS

最後建立一個scene並執行它


  CCScene *pScene = HelloWorld::scene();
  pDirector->runWithScene(pScene);

到這裡程式的執行點會移到HelloWorldScene類別中,另外applicationDidEnterBackground()為當程式移動到背景時做的處理,如手機來電等等的情況,applicationWillEnterForeground()處理程式的執行權恢復後需要的動作,裡面分別處理暫停遊戲和背景音樂

請看 HelloWorldScene.cpp中的scene()方法,事實上它建立並回傳一個指向CCScene的指標,而

  HelloWorld *layer = HelloWorld::create();//建立一個CCLayer指標 layer


  scene->addChild(layer);//把layer加入scene

這樣就完成之前所提到的CCLayer加到CCScene上,接著在 init() 方法 則是把需要的物件加到CCLayer上,其中


        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);

為左下角開關按鈕


        CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
        CC_BREAK_IF(! pLabel);

        // Get window size and place the label upper.
        CCSize size = CCDirector::sharedDirector()->getWinSize();
        pLabel->setPosition(ccp(size.width / 2, size.height - 50));

        // Add the label to HelloWorld layer as a child layer.
        this->addChild(pLabel, 1);

為 HelloWorld 字串


        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2, size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite, 0);

為中間的圖案

你可以改變這些字串內容或圖形等等,由於HelloWorld範例相當簡單,也只有2個類別,對於想要了解基本內容的各位們是相當好的教材~

標籤: