Cocos2d-x手機遊戲開發中-組合動作,cocos2dx遊戲開發
動作往往不是單一,而是複雜的組合。我們可以按照一定的次序將上述基本動作組合起來,形成連貫的一套組合動作。組合動作包括以下幾類:順序、並列、有限次數重複、無限次數重複、反動作和動畫。動畫我們會在下一節介紹,本節我們重點順序、並列、有限次數重複、無限次數重複和反動
下面我們通過一個執行個體介紹一下組合動作的使用,這個執行個體如所示,是一個操作菜單情境,選擇菜單可以進入到動作情境,在動作情境中點擊Go按鈕可以執行我們選擇的動作效果,點擊Back按鈕可以返回到菜單情境。
下面我們再看看具體的程式碼,首先看一下看HelloWorldScene.h檔案,它的代碼如下:
#ifndef __HELLOWORLD_SCENE_H__#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"#include "MyActionScene.h" ① typedef enum { ② kSequence = 100, kSpawn, kRepeate, kRepeatForever1, kReverse} ActionTypes; ③ class HelloWorld : public cocos2d::Layer{public: static cocos2d::Scene* createScene(); virtual bool init(); void OnClickMenu(cocos2d::Ref* pSender); ④ CREATE_FUNC(HelloWorld);}; #endif // __HELLOWORLD_SCENE_H__
上述代碼是中第①行是引入標頭檔MyActionScene.h。第②~③是定義個枚舉類型ActionTypes,枚舉類型ActionTypes中定義了5個常量,這5個常量對應5個功能表項目。第④行聲明了一個函數,用來在選擇不同菜單時候的回調。
在上訴代碼大家比較熟悉了,我們這裡就不再介紹了。下面我們再看看下一個情境MyActionScene,它的MyActionScene.h代碼如下:
#ifndef __MYACTION_SCENE_H__#define __MYACTION_SCENE_H__ #include "cocos2d.h"#include "HelloWorldScene.h" class MyAction : public cocos2d::Layer{ cocos2d::Sprite *sprite; public: staticcocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(MyAction); void goMenu(cocos2d::Ref* pSender); void backMenu(cocos2d::Ref* pSender); voidOnSequence(cocos2d::Ref* pSender); ① void OnSpawn(cocos2d::Ref* pSender); void OnRepeat(cocos2d::Ref* pSender); void OnReverse(cocos2d::Ref* pSender); void OnRepeatForever(cocos2d::Ref* pSender); ②}; #endif // __MYACTION_SCENE_H__
在.h檔案中第①~②行代碼是聲明了5個用於菜單選擇的回呼函數。
MyActionScene的實現代碼MyActionScene.ccp檔案,其中點擊Go按鈕時候調用的函數MyAction::goMenu代碼如下:
void MyAction::goMenu(Ref* pSender){ log("Tag= %i",this->getTag()); switch (this->getTag()) { casekSequence: this->OnSequence(pSender); break; casekSpawn: this->OnSpawn(pSender); break; casekRepeate: this->OnRepeat(pSender); break; casekRepeatForever1: this->OnRepeatForever(pSender); break; case kReverse: this->OnReverse(pSender); break; default: break; }}
我們在這個函數中根據選擇菜單不同調用不同的函數。
MyActionScene.ccp中MyActionLayer::OnSequence代碼如下:void MyAction::OnSequence(Ref* pSender){ Size size = Director::getInstance()->getVisibleSize(); Pointp = Point(size.width/2, 200); FiniteTimeAction* ac0 =(FiniteTimeAction*)sprite->runAction(Place::create(p)); ① FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction( MoveTo::create(2,Point(size.width- 130, size.height - 200))); ② FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction( JumpBy::create(2,Point(8, 8),6, 3)); ③ FiniteTimeAction* ac3 =(FiniteTimeAction*)sprite->runAction(Blink::create(2,3)); ④ FiniteTimeAction* ac4 = (FiniteTimeAction*)sprite->runAction( TintBy::create(0.5,0,255,255)); ⑤ sprite->runAction(Sequence::create(ac0,ac1, ac2, ac3, ac4, ac0, NULL)); ⑥ }
上述代碼實現了順序動作示範,其中主要使用的類是Sequence,Sequence是派生於ActionInterval屬性間隔動作。Sequence作用就是順序排列若干個動作,然後按先後次序逐個執行。代碼⑥行執行Sequence,Sequence的create函數需要動作數組。代碼第①行建立Place動作,由於Sequence的create函數需要FiniteTimeAction類型的動作,因此需要將運算式sprite->runAction(Place::create(p))強制轉換為FiniteTimeAction*類型。類似的代碼第②~⑤行都需要強制類型轉換。第②行代碼是建立MoveTo動作,第③行代碼是建立JumpBy動作。第④行代碼是建立Blink動作。第⑤行代碼是建立TintBy動作。
MyActionScene.ccp中MyActionLayer::OnSpawn,這個函數是在示範並列動作時候調用的函數,它的代碼如下:
void MyAction::OnSpawn(Ref* pSender){ Size size = Director::getInstance()->getVisibleSize(); Pointp = Point(size.width/2, 200); sprite->setRotation(0); ① sprite->setPosition(p); ② FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction( MoveTo::create(2,Point(size.width- 100, size.height - 100))); ③ FiniteTimeAction* ac2 =(FiniteTimeAction*)sprite->runAction(RotateTo::create(2, 40)); ④ sprite->runAction(Spawn::create(ac1,ac2,NULL)); ⑤ }
上述代碼實現了並列動作示範,其中主要使用的類是Spawn類也從ActionInterval繼承而來,該類作用就是同時並列執行若干個動作,但要求動作都必須 是可以同時執行的。比如:移動式翻轉、改變色、改變大小等。第⑤行代碼sprite->runAction(Spawn::create(ac1,ac2,NULL))執行並列動作,create函數的動作類型數組。第①行代碼sprite->setRotation(0)設定精靈旋轉角度保持原來狀態。第②行代碼sprite->setPosition(p)是重新設定精靈位置。第③行代碼建立MoveTo動作。第④行代碼建立RotateTo動作。
MyActionScene.ccp中MyActionLayer::OnRepeat,這個函數是在示範重複動作時候調用的函數,它的代碼如下:
void MyAction::OnRepeat(Ref* pSender){ Size size = Director::getInstance()->getVisibleSize(); Pointp = Point(size.width/2, 200); sprite->setRotation(0); sprite->setPosition(p); FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction( MoveTo::create(2,Point(size.width- 100, size.height - 100))); ① FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction( JumpBy::create(2,Point(10,10), 20,5)); ② FiniteTimeAction* ac3 = (FiniteTimeAction*)sprite->runAction( JumpBy::create(2,Point(-10,-10),20,3)); ③ ActionInterval* seq = Sequence::create(ac1, ac2, ac3, NULL); ④ sprite->runAction(Repeat::create(seq,3)); ⑤ }
上述代碼實現了重複動作示範,其中主要使用的類是Repeat類也從ActionInterval繼承而來。第①行代碼是建立MoveTo動作。第②行代碼是建立JumpBy動作。第③行代碼是建立JumpBy動作。第④行代碼是建立順序動作對象seq,seq的類型為ActionInterval*或FiniteTimeAction*。第⑤行代碼sprite->runAction(Repeat::create(seq,3)) 重複運行順序動作3次,create函數參數的類型是FiniteTimeAction。
MyActionScene.ccp中MyActionLayer::OnRepeatForever,這個函數是在示範無限重複動作時候調用的函數,它的代碼如下:
void MyAction::OnRepeatForever(Ref*pSender){ Size size = Director::getInstance()->getVisibleSize(); Point p = Point(size.width/2, 500); sprite->setRotation(0); sprite->setPosition(p); ccBezierConfigbezier; ① bezier.controlPoint_1 = Point(0, size.height/2); bezier.controlPoint_2= Point(10, -size.height/2); bezier.endPosition= Point(10,20); ② FiniteTimeAction* ac1 =(FiniteTimeAction*)sprite->runAction(BezierBy::create(2,bezier)); ③ FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction( TintBy::create(0.5,0, 255, 255)); ④ FiniteTimeAction* ac1Reverse = ((ActionInterval*)ac1)->reverse(); ⑤ FiniteTimeAction* ac2Repeat = (FiniteTimeAction*)sprite->runAction( Repeat::create((ActionInterval*)ac2,4)); ⑥ FiniteTimeAction* ac3 = (FiniteTimeAction*)sprite->runAction( Spawn::create(ac1,ac2Repeat,NULL)); ⑦ FiniteTimeAction* ac4 = (FiniteTimeAction*)sprite->runAction( Spawn::create(ac1Reverse,ac2Repeat,NULL)); ⑧ ActionInterval* seq = Sequence::create(ac3, ac4, NULL); ⑨ sprite->runAction(RepeatForever::create(seq)); ⑩ }
上述代碼實現了重複動作示範,其中主要使用的類是RepeatForever,它也從ActionInterval繼承而來。第⑩行代碼sprite->runAction(RepeatForever::create(seq))是執行無限重複動作,create函數參數的類型是FiniteTimeAction。代碼第①~②行是定義貝茲路徑控制點。第③行代碼建立貝茲路徑動作BezierBy。第④行代碼建立動作TintBy。第⑤行代碼是建立BezierBy動作的反轉動作。第⑥行代碼是建立重複動作。第⑦和⑧行代碼是建立並列動作。第⑨行代碼是建立順序動作。
MyActionScene.ccp中MyActionLayer::OnReverse,這個函數是在示範反動作時候調用的函數,它的代碼如下:
void MyAction::OnReverse(Ref* pSender){ Size size = Director::getInstance()->getVisibleSize(); Pointp = Point(size.width/2, 300); sprite->setRotation(0); sprite->setPosition(p); FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction( MoveBy::create(2,Point(40,60))); ① Action* ac2 = ac1->reverse(); ② ActionInterval* seq = Sequence::create(ac1, ac2, NULL); ③ sprite->runAction(Repeat::create(seq,2)); ④ }
上述代碼實現了反動作示範,支援順序動作的反順序動作,反順序動作不是一個類,不是所有的動作類都支援反動作。XxxTo 類通常不支援反動作,XxxBy類通常支援。第①行代碼是建立一個移動MoveBy動作。第②行代碼調用ac1的reverse()函數執行反動作。第③行代碼是建立順序動作。第④行代碼sprite->runAction(Repeat::create(seq,2))是執行反動作。
更多內容請關注Cocos2d-x系列圖書《Cocos2d-x實戰(卷Ⅰ):C++開發》本書交流討論網站:http://www.cocoagame.net歡迎加入cocos2d-x技術討論群:257760386、327403678
用cocos2d-x設計遊戲 高分跪求達人同濟西南十178找我了
cocos2d-x+vs是不是就能開發安卓的遊戲了?cocos2d-xwin32平台上開發出來代碼還需要交叉編譯才能產生android使用包具體操作見文檔
http://wenku.baidu.com/view/38927d6da98271fe910ef99b.html
另:使用cocos2d-x引擎優勢於便於移植性其開發出C++代碼只要各平台上只要稍加改動使用而單使用ECLIPSE或Xcode環境使用語言分別java和Object-c直接讓們相互轉換難度肯定要大多