閱讀807 返回首頁    go 技術社區[雲棲]


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

動作往往不是單一而是複雜的組合。我們可以按照一定的次序將上述基本動作組合起來形成連貫的一套組合動作。組合動作包括以下幾類順序、並列、有限次數重複、無限次數重複、反動作和動畫。動畫我們會在下一節介紹本節我們重點順序、並列、有限次數重複、無限次數重複和反動

下麵我們通過一個實例介紹一下組合動作的使用這個實例如下圖所示下圖是一個操作菜單場景選擇菜單可以進入到下圖動作場景在下圖動作場景中點擊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));                             ⑥
 
}

上述代碼實現了順序動作演示其中主要使用的類是SequenceSequence是派生於ActionInterval屬性間隔動作。Sequence作用就是順序排列若幹個動作然後按先後次序逐個執行。代碼⑥行執行SequenceSequence的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動作。第④行代碼是創建順序動作對象seqseq的類型為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))是執行反動作。


更多內容請關注最新Cocos圖書《Cocos2d-x實戰 C++卷》
本書交流討論網站https://www.cocoagame.net
更多精彩視頻課程請關注智捷課堂Cocos課程https://v.51work6.com
歡迎加入Cocos2d-x技術討論群257760386


《Cocos2d-x實戰 C++卷》現已上線各大商店均已開售

京東https://item.jd.com/11584534.html

亞馬遜https://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU

當當https://product.dangdang.com/23606265.html

互動出版網https://product.china-pub.com/3770734

《Cocos2d-x實戰 C++卷》源碼及樣章下載地址

源碼下載地址https://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1 

樣章下載地址https://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1

歡迎關注智捷iOS課堂微信公共平台

最後更新:2017-04-03 05:38:58

  上一篇:go actionbar詳解(二)
  下一篇:go 申請apple開發者賬號的波折