下面咱們經過一個實例介紹一下組合動做的使用,這個實例以下圖所示,下圖是一個操做菜單場景,選擇菜單能夠進入到下圖動做場景,在下圖動做場景中點擊Go按鈕能夠執行咱們選擇的動做效果,點擊Back按鈕能夠返回到菜單場景。php
下面咱們再看看具體的程序代碼,首先看一下看HelloWorldScene.h文件,它的代碼以下:html
[html] view plaincopy數組
#ifndef __HELLOWORLD_SCENE_H__ 函數
#define __HELLOWORLD_SCENE_H__ 動畫
#include "cocos2d.h" 網站
#include "MyActionScene.h" ① this
typedef enum { ② spa
kSequence = 100, .net
kSpawn, code
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代碼以下:
[html] view plaincopy
#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代碼以下:
[html] view plaincopy
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;
}
}
咱們在這個函數中根據選擇菜單不一樣調用不一樣的函數。
[html] view plaincopy
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,這個函數是在演示並列動做時候調用的函數,它的代碼以下:
[html] view plaincopy
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,這個函數是在演示重複動做時候調用的函數,它的代碼以下:
[html] view plaincopy
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,這個函數是在演示無限重複動做時候調用的函數,它的代碼以下:
[html] view plaincopy
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,這個函數是在演示反動做時候調用的函數,它的代碼以下:
[html] view plaincopy
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技術討論羣:25776038六、327403678