Cocos2d-x 架構介紹

Cocos2d-x  架構介紹

Cocos2d-x中主要包含以下4個部分:1、CCDirector導演  ,2、CCScene場景,3、

CCLayer層,4、CCSprite精靈



CCScene場景

場景類就是CCScene。

場景是什麼?

我們來舉一個例子。假設現在有一個簡單的遊戲

(1)首先,會有一個開場的歡迎界面。

(2)然後,會進入遊戲的主界面。

由此可見,玩家玩遊戲的過程就是不斷地在已經預設好的界面之間根據玩家的操作進行跳轉。這些構成整個流程的界面就是場景。


CCLayer層


場景是通過不同層的疊加和組合協作來實現不同功能的,所以層是我們遊戲的重點大約99%以上的時間是在層上實現遊戲內容。

例如:在一個場景中,最下面是背景層、其上面有菜單選項層,這就是層的疊加。



CCSprite精靈


精靈是遊戲開發的主要對象,我們在遊戲中的主人公、怪物都是精靈。


CCDirector導演


CCDirector導演類主要負責遊戲整個過程中場景的切換等。









第二章基礎知識



CCNode類

CCNode是cocosd-x的渲染鏈,開發遊戲基本上就是和它打交道了,cocosd-x同時只能渲染一個層,因此CCScene是渲染的根節點。

CCNode幾乎是遊戲中大部分類的父類。



CCLabel控件

bool HelloWorld::init()
{
    bool  pRet = false;
    do {
                                                                                                            
        CC_BREAK_IF(!CCLayer::init());
//              CCLabelTTF 每次調用 setString (即改變文字)的時候,一個新的OPENGL 紋理將會被創建.。這意味着setString 和創建一個新的標籤一樣慢。
                                                                                                            
//          所以,當你需要頻繁的更新它們的時候,儘可能的不用去使用標籤對象。  而應該使用CCLabelAtlas或者是CCLabelBMFont。
        CCLabelTTF* label = CCLabelTTF::create("LabelTTF", "Marker Felt", 21);
        label->setPosition(ccp(100, 400));
        this->addChild(label);
                                                                                                            
//         CCLabelBMFont  相當於每次改變只改變了圖片座標,而CCLabelTTF要重新渲染.這個類使用之前,需要添加好字體文件,包括一個圖片文件 (**.png) 和一個 字體座標文件 (**.fnt)。
        CCLabelBMFont* label2 = CCLabelBMFont::create("CCLabelBMFont", "font_red_14.fnt");
        label2->setPosition(ccp(200, 400));
        this->addChild(label2);
                                                                                                            
//          因爲幀率一直在變,使用CCLabelTTF的話效率太低,因爲只是數字所以也犯不上使用 CCLabelBMFont 加載那麼大的文字圖像,所以使用這個比較合適。
        CCLabelAtlas* label3 = CCLabelAtlas::create("0.0", "fps_p_w_picpaths.png", 16, 24, '.');
        label3->setPosition(ccp(200, 300));
        this->addChild(label3);
                                                                                                            
        pRet =true;
    } while (0);
    return pRet;
}

代碼下載: http://pan.baidu.com/share/link?shareid=112332870&uk=3189484501


界面切換

首先,創建一個C++的類,作爲第二個場景。

SecondScene.h

#include "cocos2d.h"
using namespace cocos2d;
class SecondScene:public CCLayerColor
{
                                                            
public:
    CREATE_FUNC(SecondScene);
                                                            
                                                            
    virtual bool init();
                                                            
    static CCScene* scene();
                                                         
};

SecondScene.cpp

#include "SecondScene.h"
CCScene* SecondScene::scene()
{
    CCScene* scene = CCScene::create();
                                                    
    SecondScene* layer = SecondScene::create();
    scene->addChild(layer);
    return scene;
}
bool SecondScene::init()
{
    bool bRet = false;
    do {
                                                        
        CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(0, 0, 0, 0)));
        CCLabelTTF* menuLabel = CCLabelTTF::create("第二個界面", "Marker Felt", 20);
        menuLabel->setColor(ccc3(255, 0, 255));
        menuLabel->setPosition(ccp(100, 300));
        this->addChild(menuLabel);
        bRet = true;
    } while (0);
    return bRet;
}




HelloWorldScene.h文件如下:

#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();
    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    void replaceScene();
    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
};


HelloWorldScene.cpp

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
  // 菜單類CCMenu  不能夠單獨使用,它是一個菜單項CCMenuItem的容器。所以我們主要研究的對象是CCMenuItem
 //CCMenuItem的常用子類有:CCMenuItemLabel和CCMenuItemSprite
    CCLabelTTF* menuLabel = CCLabelTTF::create("界面切換", "Marker Felt", 20);
                                         
     //CCMenuItemLabel創建button  第一個參數的是指將一個label對象作爲參數傳遞進去  第二個參數是指調用方法類的指針一般爲this指當前類   第三個參數是指點擊按鈕之後調用的方法
    CCMenuItemLabel* menuItem = CCMenuItemLabel::create(menuLabel, this, menu_selector(HelloWorld::replaceScene));
    //在創建CCMenu時,結尾一定要加上NULL,否則編譯時不報錯,運行時會崩潰,但是運行時會崩潰。
    CCMenu* menu = CCMenu::create(menuItem,NULL);
    menu->setPosition(ccp(100, 400));
    this->addChild(menu);
    return true;
}
void HelloWorld::replaceScene()
{
    CCDirector::sharedDirector()->replaceScene(SecondScene::scene());
                                         
}

代碼下載: http://pan.baidu.com/share/link?shareid=883177378&uk=3189484501


schedule消息調度

bool HelloWorld::init()
{
    bool bRet = false;
    do {
        CC_BREAK_IF(!CCLayer::init());
                       
        //設置一個定時器
        this->schedule(schedule_selector(HelloWorld::stopUpdate), 1);
                       
        //開啓每幀更新
        this->scheduleUpdate();
        bRet = true;  
    } while (0);
    return bRet;
}

int i =0;
void HelloWorld::update(float dt)
{
    CCLog("%d",i);
    i++;
}

void HelloWorld::stopUpdate()
{
    //停止每幀更新的方法
    this->unscheduleUpdate();
}



代碼下載: http://pan.baidu.com/share/link?shareid=2828641080&uk=3189484501


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章