创建Scene场景和添加Sprite对象到场景中只是游戏开发的一部分。对于一个游戏而言,我们要让游戏中的东西能够移动。Action(动作)对象是每一个游戏中不可或缺的部分。 Actions(动作)允许Node对象在时空中转换。想从一个点移动Sprite对象到另一个点,并在完成时,调用回调,怎样可以实现呢?我们在一个Node节点上创建一个Action动作序列。这些可以是改变Node对象的position(位置), rotation(旋转)和 scale(缩放)的动作,如MoveBy, Rotate, Scale。
创建Action对象:
1、Move:MoveBy与MoveTo
例子:
MoveBy是相对原来的位置进行移动,而MoveTo则是绝对地移动。
2、Rotate:RotateBy与RotateTo
3、Scale:ScaleBy与ScaleTo
4、Fade:FadeIn与FadeOut
透明度的值变化范围为0 到 255。
5、Tint(色调):TintBy与TintTo
给一个实现了NodeRGB协议的Node(节点)着色,从当前色调到自定义色调。
6、Animate(动画)
使用Animate很容易实现Spritec对象的翻页动画。
7、Easing (缓冲)
Easing是通过指定加速让动画变得平滑的动画制作。
Cocos2d-x支持绝大部分缓冲功能。
例子:
Sequences序列
Sequences就是一系列的Action对象被有顺序地执行。序列中可以是任意数量的Action对象、函数甚至其他Sequence(序列)。Cocos2d-x 有CallFunc对象,它允许你创建 function()然后传递到Sequence对象中去执行。
上面的代码中的动作的执行顺序:
Jump -> callbackJump() -> Rotate -> callbackRotate()
Delays in Sequences
当你想Sequence中的Action之间有一定的延迟,可以使用CallFunc来实现:
Spawn
Spawn与Sequence相似,但Spawn的动作是同时执行的。Spawn产生的结果与连续执行多个runAction()语句的结果是一样的。Spawn的好处是你可以将它放入Sequence中去执行,可以获得特定的效果。
方式一:
方式二:
方式一与方式二是等价的。
在Sequence中使用Spawn
Cloned克隆
如果我们有一个Action对象,那么我们可以调用clone()函数,将其应用到多个Node(节点)对象上。为什么要用克隆呢? Action对象都有一个内部状态(internal state)。当它们运行时,它们是真正去改变Node(节点)对象的属性。如果不使用clone(),你无法真正拥有一个唯一的Action对象来应用到Node(节点)对象上。那就将会导致一些不是我们所期望的结果。
Reverse逆序
如果我们有一系列Action对象,我们可以调用reverse()逆序执行它们。调用reverse(),实际是逆顺序处理Sequence或Spawn的原始属性。
大多数的 Action和Sequence对象都系可以逆序的。
例子: