有了Action(MoveTo,ScaleBy等)的基础动作,cocos为我们提供了一些方法,把这些动作连起来。
Sequence
按顺序或者说是按序列的执行动作。
比如下面的这段代码:
auto jump = JumpBy::create(1.5, Vec2(0, 0), 100, 3);
auto rotate = RotateTo::create(2.0f, 90);
sprite1->runAction(Sequence::create(jump, rotate, nullptr));
它描述了,先jump 后 rotate这样的动作顺序。callback是啥,sequence不光可以加载定义好的各种动作,还可以加载自己定义的动作,这就是CallFunc定义的东西。这里面只是输出了log。(输出的log内容只能在调试下可以看到,也就是只按F5)
各个动作之间如果想加入一些延迟怎么弄?可以用DelayTime。下面这样就是在跳跃和旋转停顿5秒。
DelayTime* delay = DelayTime::create(5);
sprite1->runAction(Sequence::create(jump, callbackJump, delay, rotate, callbackRotate, nullptr));
Spawn
Sequence是顺序执行的,而Spawn是并列执行的。也就是放入Spawn里面的动作是一起被执行的。
auto moveBy = MoveBy::create(2, Vec2(400, 100));
auto fadeTo = FadeTo::create(2.0f, 120.0f);
auto mySpawn = Spawn::createWithTwoActions(moveBy, fadeTo);
sprite1->runAction(mySpawn);
那么我们有了这两种工具,还可以把Spawn嵌入到Sequence里,实现更多的效果:
auto moveBy = MoveBy::create(1, Vec2(400, 100));
auto fadeTo = FadeTo::create(2.0f, 120.0f);
auto scaleBy = ScaleBy::create(2.0f, 3.0f);
auto mySpawn = Spawn::createWithTwoActions(scaleBy, fadeTo);
sprite1->runAction(Sequence::create(moveBy, mySpawn, nullptr));
RepeatForever
RepeatForever会让动作永远的执行下去,用法也很简单:
auto moveBy = MoveBy::create(1.0f, Vec2(500, 0));
auto scaleBy = ScaleBy::create(1.0f, 2.0f);
auto mySpawn = Spawn::createWithTwoActions(moveBy, scaleBy);
auto sequence = Sequence::create(mySpawn, mySpawn->reverse(), nullptr);
sprite1->runAction(RepeatForever::create(sequence));
那么,想要控制循环动作几次呢?只需要用Repeat就行了:
sprite1->runAction(Repeat::create(sequence, 3)); //重复执行3次