關於Cocos2d x專案執行的過程和場景切換步驟

2021-09-07 06:53:24 字數 1655 閱讀 3197

這個函式裡面的語句

auto scene = helloworld::createscene();    //建立乙個helloworld型別的場景,其實是乙個帶著層,層中帶著節點的場景

// run

director->runwithscene(scene);  //執行這個場景

2.helloworld類是乙個繼承自層的類,.h檔案裡面有三個函式的宣告,第二個和第三個是自己的初始化函式和建構函式,第乙個是建立場景特意寫的乙個函式宣告,helloworld是乙個繼承自層卻可以建立場景的特殊類

static cocos2d::scene* createscene();

virtual bool init();

create_func(helloworld);

3.helloworld.cpp檔案有兩個函式的定義,分別是.h檔案裡面第乙個和第二個函式的定義,把自己的建構函式的定義省略了

scene* helloworld::createscene()

auto rootnode = csloader::createnode("mainscene.csb");  //建立乙個節點(其實可以是各種型別的節點,node,sprite...)

addchild(rootnode);  //新增節點到層例項

return true;  //init成功,返回true

場景切換的步驟

1.要實現切換就要有多個場景,所以建立一些場景類,像gamestar和gameover還有gamescene,這些場景類是繼承自層的,且它的寫法要和helloworld類的寫法一樣,在.h檔案中宣告的函式要有三個,可以對函式引數進行適當的新增和改變。

static cocos2d::scene* createscene();

virtual bool init();

create_func(gameover);

2.在場景類的.cpp檔案中對這三個函式進行定義

scene* gameover::createscene()

auto sprite = sprite::create("gameover.png");  //建立乙個節點(其實可以是各種型別的節點,node,sprite...)

sprite->setposition(50,50);

addchild(sprite);  //新增節點到層例項

return true;  //init成功,返回true

3.在某個事件***中用這個語句切換,當判定遊戲結束時就切換到遊戲結束介面

director::getinstance()->replacescene(gameover::createscene(_herocount, 20));

有時候我們要讓場景切換附加某種特效,比如淡入淡出,就用

auto scene = gamescene::createscene();

director::getinstance()->replacescene(transitionfade::create(2, scene));;

注意:剛開始的時候,節點的新增可以直接在helloworld的init函式的定義中直接auto相應的物件並獲得例項,再把例項新增到helloworld層中來顯示,這裡還要設定節點的位置(節點的大小還有其他屬性在各自類的init函式中定義),後期要把這些節點的新增**另外用乙個gamecontroller類來管理

Python建立cocos2dx專案

二 配置環境變數 在path路徑下新增e python,此處e是安裝python所在盤 四 執行 cmd 定位到解壓的cocos2xd的資料夾下 e cocos2d x 2.2.2 cocos2d x 2.2.2 tools project creator 執行 python create proj...

關於cocos2dx中tableView的一些理解

先看 h檔案中 ifndef helloworld scene h define helloworld scene h include cocos2d.h include cocos ext.h using ns cc using ns cc ext class helloworld public ...

cocos2dx筆記 關於action

1.基本動作instantaction 放置 place 隱藏 hide 顯示 show 可見切換 togglevisibility 2.延時動作 移動到 ccmoveto 移動 ccmoveby 跳躍到 ccjumpto 跳躍 ccjumpby 貝塞爾 ccbezierby 放大到 ccscale...