Action

Actions 
Actions are lightweight classes that are used on nodes to perform certain, well, actions. 
They allow you to move, rotate, scale, tint, fade, and do a lot of other things with a node. 
Because they work with every node, you can use them on sprites, labels, even menus or 
whole scenes! That’s what makes them so powerful. 
Since most actions happen over time, like a rotation for three seconds, you’d normally 
have to write an update method and add variables to store the intermediate results. 
Actions wrap this kind of logic for you and turn it into simple, parameterized methods: 
// I want myNode to move to 100, 200 and arrive there in 3 seconds 
CCMoveTo* move = [CCMoveTo actionWithDuration:3 position:CGPointMake(100, 200)]; 
[myNode runAction:move]; 
Actions come in two flavors. Instant actions are basically the same as setting a node 
property like visible or flipX. Interval actions run over a period of time, like the move 
action above. By the way, you don’t have to remove either type of action. Once an 
action has completed its task, it will remove itself from the node automatically and 
releases the memory it uses. 

Repeating Actions 
You can also have actions or even a whole sequence of actions repeat forever. You can
create endlessly looping animations this way. This code lets a node rotate forever like a
endlessly spinning wheel: 
CCRotateBy* rotateBy = [CCRotateBy actionWithDuration:2 angle:360]; 
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:rotateBy]; 
[myNode runAction:repeat]; 
Ease Actions 
Actions become even more powerful by using CCEaseAction class actions. Ease actions
allow you to modify the effect of an action over time. For example, if you use a CCMoveTo
action on a node, the node will move the whole distance at the same speed until it has 
arrived. With CCEaseAction, you can have the node start slow and speed up towards th
target, or vice versa. Or you can let it move past the target location a little and then 
bounce back. Ease actions create very dynamic animations that are normally very time-
consuming to implement. The following code shows how an ease action is used to 
modify the behavior of a regular action. The rate parameter determines how 
pronounced the effect of the ease action is, and should be greater than 1 to see any 
effect. 
// I want myNode to move to 100, 200 and arrive there in 3 seconds 
CCMoveTo* move = [CCMoveTo actionWithDuration:3 position:CGPointMake(100, 200)]; 
// this time the node should slowly speed up and then slow down as it moves 
CCEaseInOut* ease = [CCEaseInOut actionWithAction:move rate:4]; 
[myNode runAction:ease]; 

NOTE: In the example, the ease action is run on the node, not the move action. It’s all too easy to 
forget to change the runAction line when you’re working with actions. It’s a common mistake 
that happens even to the most experienced cocos2d developers. If you notice your actions aren’t 
working as expected or not at all, double-check that you’re actually running the correct action. 
And if the correct actions are used but you’re still not seeing the desired result, verify that it’s the 
correct node running the action. This is another common mistake. 
Cocos2d implements the following CCEaseAction classes: 
> CCEaseBackIn, CCEaseBackInOut, CCEaseBackOut 
> CCEaseBounceIn, CCEaseBounceInOut, CCEaseBounceOut 
> CCEaseElasticIn, CCEaseElasticInOut, CCEaseElasticOut 
> CCEaseExponentialIn, CCEaseExpon
entialInOut, 
CCEaseExponentialOut 
> CCEaseIn, CCEaseInOut, CCEaseOut 
> CCEaseSineIn, CCEaseSineInOut, CCEaseSineOut

动作是轻量级的类,可以用来让节点执行诸如移动、旋转、缩放、变色、消失等很多
动作。由于它们能作用在所有节点上,因此可以对精灵、标签甚至菜单或整个场景施加动
作。这就是动作的强大之处。
由于动作会执行一段时间,如旋转3 秒钟,因此通常的做法是写一个更新方法,并加
一些变量来存储中间结果。动作封装了这部分的逻辑,并将之转换成了带参数的方法:
// I want myNode to move to 100, 200 and arrive there in 3 seconds
CCMoveTo* move = [CCMoveTo actionWithDuration:3 position:CGPointMake(100, 200)];
[myNode runAction:move];
动作分为两大类:瞬时动作基本上等同于设置节点的属性,如visible 和flipX;延时
动作会执行一段时间,如上面的移动动作。另外,不需要清除这两种动作。一旦动作完成,
就会自动从节点上清除并释放它所占用的内存。

3.10.1 重复动作
可以使一个动作或一系列的动作不停地重复,用这个方法可以创建无限循环的动画。
下面这段代码让一个节点像轮子一样不停旋转:
CCRotateBy* rotateBy = [CCRotateBy actionWithDuration:2 angle:360];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:rotateBy];
[myNoderunAction:repeat];

CCEaseAction 类的出现使得动作显得更为强大了。流畅动作允许你改变在一段时间内
的动作效果。例如,如果你对一个节点使用CCMoveTo,它会匀速移动到目标点。如果使
用CCEaseAction,就可以让节点由慢到快或由快到慢地移向目标。或者让节点移过目的地
一些,再弹回来。流畅动作能帮你创建通常要花很长时间才能做出来的动画。下面这段代
码展示了如何使用流畅动作来改变一个普通动作的行为(rate 参数决定了流畅动画的明显程
度,只有当它大于1 时才能看到效果):
// I want myNode to move to 100, 200 and arrive there in 3 seconds
CCMoveTo* move = [CCMoveTo actionWithDuration:3 position:CGPointMake(100, 200)];
// this time the node should slowly speed up and then slow down as it moves
CCEaseInOut* ease = [CCEaseInOut actionWithAction:move rate:4];
[myNode runAction:ease];
注意

cocos2d 实现了如下几个CCEaseAction 类:
● CCEaseBackIn、CCEaseBackInOut、CCEaseBackOut
● CCEaseBounceIn、CCEaseBounceInOut、CCEaseBounceOut
● CCEaseElasticIn、CCEaseElasticInOut、CCEaseElasticOut
● CCEaseExponentialIn、CCEaseExponentialInOut、CCEaseExponentialOut
● CCEaseIn、CCEaseInOut、CCEaseOut
● CCEaseSineIn、CCEaseSineInOut、CCEaseSineOut

转载地址:http://zhouguangwu.sinaapp.com/detail.php?id=160

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值