Cocos2d-x手機遊戲開發中-組合動做

動做每每不是單一,而是複雜的組合。咱們能夠按照必定的次序將上述基本動做組合起來,造成連貫的一套組合動做。組合動做包括如下幾類:順序、並列、有限次數重複、無限次數重複、反動做和動畫。動畫咱們會在下一節介紹,本節咱們重點順序、並列、有限次數重複、無限次數重複和反動

下面咱們經過一個實例介紹一下組合動做的使用,這個實例以下圖所示,下圖是一個操做菜單場景,選擇菜單能夠進入到下圖動做場景,在下圖動做場景中點擊Go按鈕能夠執行咱們選擇的動做效果,點擊Back按鈕能夠返回到菜單場景。php


下面咱們再看看具體的程序代碼,首先看一下看HelloWorldScene.h文件,它的代碼以下:html

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片數組

  1. #ifndef __HELLOWORLD_SCENE_H__  函數

  2. #define __HELLOWORLD_SCENE_H__  動畫

  3.    

  4. #include "cocos2d.h"  網站

  5. #include "MyActionScene.h"                                                                                                                ①  this

  6.    

  7. typedef enum {                                                                                                                             ②  spa

  8.    kSequence = 100,  .net

  9.    kSpawn,  code

  10.    kRepeate,  

  11.    kRepeatForever1,  

  12.    kReverse  

  13. } ActionTypes;                                                                                                                             ③  

  14.    

  15. class HelloWorld : public cocos2d::Layer  

  16. {  

  17. public:  

  18.    

  19.    static cocos2d::Scene* createScene();  

  20.    virtual bool init();   

  21.      

  22.    void OnClickMenu(cocos2d::Ref* pSender);                                                                              ④  

  23.      

  24.    CREATE_FUNC(HelloWorld);  

  25. };  

  26.    

  27. #endif // __HELLOWORLD_SCENE_H__  


上述代碼是中第①行是引入頭文件MyActionScene.h。第②~③是定義個枚舉類型ActionTypes,枚舉類型ActionTypes中定義了5個常量,這5個常量對應5個菜單項。第④行聲明瞭一個函數,用來在選擇不一樣菜單時候的回調。

在上訴代碼你們比較熟悉了,咱們這裏就再也不介紹了。下面咱們再看看下一個場景MyActionScene,它的MyActionScene.h代碼以下:

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. #ifndef __MYACTION_SCENE_H__  

  2. #define __MYACTION_SCENE_H__  

  3.    

  4. #include "cocos2d.h"  

  5. #include "HelloWorldScene.h"  

  6.    

  7. class MyAction : public cocos2d::Layer  

  8. {  

  9.    cocos2d::Sprite *sprite;  

  10.    

  11. public:  

  12.      

  13.     staticcocos2d::Scene* createScene();  

  14.    virtual bool init();  

  15.    CREATE_FUNC(MyAction);  

  16.      

  17.    void goMenu(cocos2d::Ref* pSender);  

  18.    void backMenu(cocos2d::Ref* pSender);  

  19.    

  20.         voidOnSequence(cocos2d::Ref* pSender);                                                                              ①  

  21.    void OnSpawn(cocos2d::Ref* pSender);  

  22.    void OnRepeat(cocos2d::Ref* pSender);  

  23.    void OnReverse(cocos2d::Ref* pSender);  

  24.    void OnRepeatForever(cocos2d::Ref* pSender);                                                            ②  

  25. };  

  26.    

  27. #endif // __MYACTION_SCENE_H__  


在.h文件中第①~②行代碼是聲明瞭5個用於菜單選擇的回調函數。

MyActionScene的實現代碼MyActionScene.ccp文件,其中點擊Go按鈕時候調用的函數MyAction::goMenu代碼以下:

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. void MyAction::goMenu(Ref* pSender)  

  2. {    

  3.     log("Tag= %i",this->getTag());  

  4.    switch (this->getTag()) {  

  5.          casekSequence:  

  6.              this->OnSequence(pSender);  

  7.              break;  

  8.          casekSpawn:  

  9.             this->OnSpawn(pSender);  

  10.              break;  

  11.          casekRepeate:  

  12.             this->OnRepeat(pSender);  

  13.              break;  

  14.          casekRepeatForever1:  

  15.             this->OnRepeatForever(pSender);  

  16.              break;  

  17.          case kReverse:  

  18.              this->OnReverse(pSender);  

  19.              break;  

  20.        default:  

  21.              break;  

  22.     }  

  23. }  


咱們在這個函數中根據選擇菜單不一樣調用不一樣的函數。

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. MyActionScene.ccp中MyActionLayer::OnSequence代碼以下:  

  2. void MyAction::OnSequence(Ref* pSender)  

  3. {  

  4.    Size size = Director::getInstance()->getVisibleSize();  

  5.         Pointp = Point(size.width/2, 200);  

  6.    

  7.    FiniteTimeAction* ac0 =(FiniteTimeAction*)sprite->runAction(Place::create(p));                  ①  

  8.    FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(  

  9.                                                 MoveTo::create(2,Point(size.width- 130, size.height - 200)));         ②  

  10.    FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction(  

  11.                                                 JumpBy::create(2,Point(8, 8),6, 3));                                          ③  

  12.    FiniteTimeAction* ac3 =(FiniteTimeAction*)sprite->runAction(Blink::create(2,3));                ④  

  13.    FiniteTimeAction* ac4 = (FiniteTimeAction*)sprite->runAction(  

  14.                                                 TintBy::create(0.5,0,255,255));                                                   ⑤  

  15.      

  16.       sprite->runAction(Sequence::create(ac0,ac1, ac2, ac3, ac4, ac0, NULL));                             ⑥  

  17.    

  18. }  


上述代碼實現了順序動做演示,其中主要使用的類是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在CODE上查看代碼片派生到個人代碼片

  1. void MyAction::OnSpawn(Ref* pSender)  

  2. {  

  3.    Size size = Director::getInstance()->getVisibleSize();  

  4.     Pointp = Point(size.width/2, 200);  

  5.      

  6.    sprite->setRotation(0);                                                                                                          ①  

  7.         sprite->setPosition(p);                                                                                                        ②  

  8.      

  9.    FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(  

  10.                            MoveTo::create(2,Point(size.width- 100, size.height - 100)));                     ③  

  11.    FiniteTimeAction* ac2 =(FiniteTimeAction*)sprite->runAction(RotateTo::create(2, 40));       ④  

  12.      

  13.    sprite->runAction(Spawn::create(ac1,ac2,NULL));                                                                      ⑤  

  14.      

  15. }  


上述代碼實現了並列動做演示,其中主要使用的類是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在CODE上查看代碼片派生到個人代碼片

  1. void MyAction::OnRepeat(Ref* pSender)  

  2. {  

  3.    Size size = Director::getInstance()->getVisibleSize();  

  4.     Pointp = Point(size.width/2, 200);  

  5.      

  6.    sprite->setRotation(0);  

  7.         sprite->setPosition(p);  

  8.      

  9.    FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(  

  10.                            MoveTo::create(2,Point(size.width- 100, size.height - 100)));                     ①  

  11.    FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction(  

  12.                            JumpBy::create(2,Point(10,10), 20,5));                                                          ②  

  13.    FiniteTimeAction* ac3 = (FiniteTimeAction*)sprite->runAction(  

  14.                            JumpBy::create(2,Point(-10,-10),20,3));                                                         ③  

  15.      

  16.    ActionInterval* seq = Sequence::create(ac1, ac2, ac3, NULL);                                         ④  

  17.      

  18.    sprite->runAction(Repeat::create(seq,3));                                                                                    ⑤  

  19.      

  20. }  


上述代碼實現了重複動做演示,其中主要使用的類是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在CODE上查看代碼片派生到個人代碼片

  1. void MyAction::OnRepeatForever(Ref*pSender)  

  2. {  

  3.    Size size = Director::getInstance()->getVisibleSize();  

  4.          Point p = Point(size.width/2, 500);  

  5.      

  6.    sprite->setRotation(0);  

  7.         sprite->setPosition(p);  

  8.      

  9.         ccBezierConfigbezier;                                                                                                         ①  

  10.    bezier.controlPoint_1 = Point(0, size.height/2);  

  11.         bezier.controlPoint_2Point(10, -size.height/2);  

  12.         bezier.endPositionPoint(10,20);                                                                                  ②  

  13.    FiniteTimeAction* ac1 =(FiniteTimeAction*)sprite->runAction(BezierBy::create(2,bezier));   ③  

  14.    

  15.    FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction(  

  16.                                 TintBy::create(0.5,0, 255, 255));                                                                           ④  

  17.    FiniteTimeAction* ac1Reverse = ((ActionInterval*)ac1)->reverse();                                          ⑤  

  18.    FiniteTimeAction* ac2Repeat = (FiniteTimeAction*)sprite->runAction(  

  19.                                 Repeat::create((ActionInterval*)ac2,4));                                                    ⑥  

  20.      

  21.    FiniteTimeAction* ac3 = (FiniteTimeAction*)sprite->runAction(  

  22.                                        Spawn::create(ac1,ac2Repeat,NULL));                                                ⑦  

  23.      

  24.    FiniteTimeAction* ac4 = (FiniteTimeAction*)sprite->runAction(  

  25.                                        Spawn::create(ac1Reverse,ac2Repeat,NULL));                                           ⑧  

  26.      

  27.    ActionInterval* seq = Sequence::create(ac3, ac4, NULL);                                                 ⑨  

  28.      

  29.    sprite->runAction(RepeatForever::create(seq));                                                                         ⑩  

  30.      

  31. }  


上述代碼實現了重複動做演示,其中主要使用的類是RepeatForever,它也從ActionInterval繼承而來。第⑩行代碼sprite->runAction(RepeatForever::create(seq))是執行無限重複動做,create函數參數的類型是FiniteTimeAction。代碼第①~②行是定義貝塞爾曲線控制點。第③行代碼建立貝塞爾曲線動做BezierBy。第④行代碼建立動做TintBy。第⑤行代碼是建立BezierBy動做的反轉動做。第⑥行代碼是建立重複動做。第⑦和⑧行代碼是建立並列動做。第⑨行代碼是建立順序動做。

MyActionScene.ccp中MyActionLayer::OnReverse,這個函數是在演示反動做時候調用的函數,它的代碼以下:

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

  1. void MyAction::OnReverse(Ref* pSender)  

  2. {  

  3.    Size size = Director::getInstance()->getVisibleSize();  

  4.         Pointp = Point(size.width/2, 300);  

  5.      

  6.    sprite->setRotation(0);  

  7.         sprite->setPosition(p);  

  8.      

  9.    FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(  

  10.                                                 MoveBy::create(2,Point(40,60)));                                                        ①  

  11.    Action* ac2 = ac1->reverse();                                                                                               ②  

  12.    

  13.    ActionInterval* seq = Sequence::create(ac1, ac2, NULL);                                                 ③  

  14.      

  15.    sprite->runAction(Repeat::create(seq,2));                                                                                    ④  

  16.      

  17. }  


上述代碼實現了反動做演示,支持順序動做的反順序動做,反順序動做不是一個類,不是全部的動做類都支持反動做。XxxTo 類一般不支持反動做,XxxBy類一般支持。第①行代碼是建立一個移動MoveBy動做。第②行代碼調用ac1的reverse()函數執行反動做。第③行代碼是建立順序動做。第④行代碼sprite->runAction(Repeat::create(seq,2))是執行反動做。


更多內容請關注Cocos2d-x系列圖書《Cocos2d-x實戰(卷Ⅰ):C++開發》

本書交流討論網站:http://www.cocoagame.net

歡迎加入cocos2d-x技術討論羣:25776038六、327403678

相關文章
相關標籤/搜索