Cocos2d-x手机游戏开发中-组合动作

时间:2024-01-14 17:04:38

动作往往不是单一,而是复杂的组合。我们可以按照一定的次序将上述基本动作组合起来,形成连贯的一套组合动作。组合动作包括以下几类:顺序、并列、有限次数重复、无限次数重复、反动作和动画。动画我们会在下一节介绍,本节我们重点顺序、并列、有限次数重复、无限次数重复和反动

下面我们通过一个实例介绍一下组合动作的使用,这个实例如下图所示,下图是一个操作菜单场景,选择菜单可以进入到下图动作场景,在下图动作场景中点击Go按钮可以执行我们选择的动作效果,点击Back按钮可以返回到菜单场景。

Cocos2d-x手机游戏开发中-组合动作

下面我们再看看具体的程序代码,首先看一下看HelloWorldScene.h文件,它的代码如下:

  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3. #include "cocos2d.h"
  4. #include "MyActionScene.h"                                                                                                                ①
  5. typedef enum {                                                                                                                             ②
  6. kSequence = 100,
  7. kSpawn,
  8. kRepeate,
  9. kRepeatForever1,
  10. kReverse
  11. } ActionTypes;                                                                                                                             ③
  12. class HelloWorld : public cocos2d::Layer
  13. {
  14. public:
  15. static cocos2d::Scene* createScene();
  16. virtual bool init();
  17. void OnClickMenu(cocos2d::Ref* pSender);                                                                              ④
  18. CREATE_FUNC(HelloWorld);
  19. };
  20. #endif // __HELLOWORLD_SCENE_H__

上述代码是中第①行是引入头文件MyActionScene.h。第②~③是定义个枚举类型ActionTypes,枚举类型ActionTypes中定义了5个常量,这5个常量对应5个菜单项。第④行声明了一个函数,用来在选择不同菜单时候的回调。

在上诉代码大家比较熟悉了,我们这里就不再介绍了。下面我们再看看下一个场景MyActionScene,它的MyActionScene.h代码如下:

  1. #ifndef __MYACTION_SCENE_H__
  2. #define __MYACTION_SCENE_H__
  3. #include "cocos2d.h"
  4. #include "HelloWorldScene.h"
  5. class MyAction : public cocos2d::Layer
  6. {
  7. cocos2d::Sprite *sprite;
  8. public:
  9. staticcocos2d::Scene* createScene();
  10. virtual bool init();
  11. CREATE_FUNC(MyAction);
  12. void goMenu(cocos2d::Ref* pSender);
  13. void backMenu(cocos2d::Ref* pSender);
  14. voidOnSequence(cocos2d::Ref* pSender);                                                                              ①
  15. void OnSpawn(cocos2d::Ref* pSender);
  16. void OnRepeat(cocos2d::Ref* pSender);
  17. void OnReverse(cocos2d::Ref* pSender);
  18. void OnRepeatForever(cocos2d::Ref* pSender);                                                            ②
  19. };
  20. #endif // __MYACTION_SCENE_H__

在.h文件中第①~②行代码是声明了5个用于菜单选择的回调函数。

MyActionScene的实现代码MyActionScene.ccp文件,其中点击Go按钮时候调用的函数MyAction::goMenu代码如下:

  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. }

我们在这个函数中根据选择菜单不同调用不同的函数。

  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. FiniteTimeAction* ac0 =(FiniteTimeAction*)sprite->runAction(Place::create(p));                  ①
  7. FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(
  8. MoveTo::create(2,Point(size.width- 130, size.height - 200)));         ②
  9. FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction(
  10. JumpBy::create(2,Point(8, 8),6, 3));                                          ③
  11. FiniteTimeAction* ac3 =(FiniteTimeAction*)sprite->runAction(Blink::create(2,3));                ④
  12. FiniteTimeAction* ac4 = (FiniteTimeAction*)sprite->runAction(
  13. TintBy::create(0.5,0,255,255));                                                   ⑤
  14. sprite->runAction(Sequence::create(ac0,ac1, ac2, ac3, ac4, ac0, NULL));                             ⑥
  15. }

上述代码实现了顺序动作演示,其中主要使用的类是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,这个函数是在演示并列动作时候调用的函数,它的代码如下:

  1. void MyAction::OnSpawn(Ref* pSender)
  2. {
  3. Size size = Director::getInstance()->getVisibleSize();
  4. Pointp = Point(size.width/2, 200);
  5. sprite->setRotation(0);                                                                                                          ①
  6. sprite->setPosition(p);                                                                                                        ②
  7. FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(
  8. MoveTo::create(2,Point(size.width- 100, size.height - 100)));                     ③
  9. FiniteTimeAction* ac2 =(FiniteTimeAction*)sprite->runAction(RotateTo::create(2, 40));       ④
  10. sprite->runAction(Spawn::create(ac1,ac2,NULL));                                                                      ⑤
  11. }

上述代码实现了并列动作演示,其中主要使用的类是Spawn类也从ActionInterval继承而来,该类作用就是同时并列执行若干个动作,但要求动作都必须 是可以同时执行的。比如:移动式翻转、改变色、改变大小等。第⑤行代码sprite->runAction(Spawn::create(ac1,ac2,NULL))执行并列动作,create函数的动作类型数组。第①行代码sprite->setRotation(0)设置精灵旋转角度保持原来状态。第②行代码sprite->setPosition(p)是重新设置精灵位置。第③行代码创建MoveTo动作。第④行代码创建RotateTo动作。

MyActionScene.ccp中MyActionLayer::OnRepeat,这个函数是在演示重复动作时候调用的函数,它的代码如下:

  1. void MyAction::OnRepeat(Ref* pSender)
  2. {
  3. Size size = Director::getInstance()->getVisibleSize();
  4. Pointp = Point(size.width/2, 200);
  5. sprite->setRotation(0);
  6. sprite->setPosition(p);
  7. FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(
  8. MoveTo::create(2,Point(size.width- 100, size.height - 100)));                     ①
  9. FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction(
  10. JumpBy::create(2,Point(10,10), 20,5));                                                          ②
  11. FiniteTimeAction* ac3 = (FiniteTimeAction*)sprite->runAction(
  12. JumpBy::create(2,Point(-10,-10),20,3));                                                         ③
  13. ActionInterval* seq = Sequence::create(ac1, ac2, ac3, NULL);                                         ④
  14. sprite->runAction(Repeat::create(seq,3));                                                                                    ⑤
  15. }

上述代码实现了重复动作演示,其中主要使用的类是Repeat类也从ActionInterval继承而来。第①行代码是创建MoveTo动作。第②行代码是创建JumpBy动作。第③行代码是创建JumpBy动作。第④行代码是创建顺序动作对象seq,seq的类型为ActionInterval*或FiniteTimeAction*。第⑤行代码sprite->runAction(Repeat::create(seq,3)) 重复运行顺序动作3次,create函数参数的类型是FiniteTimeAction。

MyActionScene.ccp中MyActionLayer::OnRepeatForever,这个函数是在演示无限重复动作时候调用的函数,它的代码如下:

  1. void MyAction::OnRepeatForever(Ref*pSender)
  2. {
  3. Size size = Director::getInstance()->getVisibleSize();
  4. Point p = Point(size.width/2, 500);
  5. sprite->setRotation(0);
  6. sprite->setPosition(p);
  7. ccBezierConfigbezier;                                                                                                         ①
  8. bezier.controlPoint_1 = Point(0, size.height/2);
  9. bezier.controlPoint_2= Point(10, -size.height/2);
  10. bezier.endPosition= Point(10,20);                                                                                  ②
  11. FiniteTimeAction* ac1 =(FiniteTimeAction*)sprite->runAction(BezierBy::create(2,bezier));   ③
  12. FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction(
  13. TintBy::create(0.5,0, 255, 255));                                                                           ④
  14. FiniteTimeAction* ac1Reverse = ((ActionInterval*)ac1)->reverse();                                          ⑤
  15. FiniteTimeAction* ac2Repeat = (FiniteTimeAction*)sprite->runAction(
  16. Repeat::create((ActionInterval*)ac2,4));                                                    ⑥
  17. FiniteTimeAction* ac3 = (FiniteTimeAction*)sprite->runAction(
  18. Spawn::create(ac1,ac2Repeat,NULL));                                                ⑦
  19. FiniteTimeAction* ac4 = (FiniteTimeAction*)sprite->runAction(
  20. Spawn::create(ac1Reverse,ac2Repeat,NULL));                                           ⑧
  21. ActionInterval* seq = Sequence::create(ac3, ac4, NULL);                                                 ⑨
  22. sprite->runAction(RepeatForever::create(seq));                                                                         ⑩
  23. }

上述代码实现了重复动作演示,其中主要使用的类是RepeatForever,它也从ActionInterval继承而来。第⑩行代码sprite->runAction(RepeatForever::create(seq))是执行无限重复动作,create函数参数的类型是FiniteTimeAction。代码第①~②行是定义贝塞尔曲线控制点。第③行代码创建贝塞尔曲线动作BezierBy。第④行代码创建动作TintBy。第⑤行代码是创建BezierBy动作的反转动作。第⑥行代码是创建重复动作。第⑦和⑧行代码是创建并列动作。第⑨行代码是创建顺序动作。

MyActionScene.ccp中MyActionLayer::OnReverse,这个函数是在演示反动作时候调用的函数,它的代码如下:

  1. void MyAction::OnReverse(Ref* pSender)
  2. {
  3. Size size = Director::getInstance()->getVisibleSize();
  4. Pointp = Point(size.width/2, 300);
  5. sprite->setRotation(0);
  6. sprite->setPosition(p);
  7. FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(
  8. MoveBy::create(2,Point(40,60)));                                                        ①
  9. Action* ac2 = ac1->reverse();                                                                                               ②
  10. ActionInterval* seq = Sequence::create(ac1, ac2, NULL);                                                 ③
  11. sprite->runAction(Repeat::create(seq,2));                                                                                    ④
  12. }

上述代码实现了反动作演示,支持顺序动作的反顺序动作,反顺序动作不是一个类,不是所有的动作类都支持反动作。XxxTo 类通常不支持反动作,XxxBy类通常支持。第①行代码是创建一个移动MoveBy动作。第②行代码调用ac1的reverse()函数执行反动作。第③行代码是创建顺序动作。第④行代码sprite->runAction(Repeat::create(seq,2))是执行反动作。

更多内容请关注Cocos2d-x系列图书《Cocos2d-x实战(卷Ⅰ):C++开发》
本书交流讨论网站:http://www.cocoagame.net
欢迎加入cocos2d-x技术讨论群:257760386、327403678