[学习笔记] Cocos Animation

本文将详细介绍如何通过cocos2d-js实现帧动画,包括定义动画的基本信息、创建并播放Action(cc.Animate),以及使用cc.textureCache定义帧动画的方法。通过四个步骤帮助开发者轻松掌握帧动画的实现。

要通过cocos2d-js实现帧动画,需要完成以下基本操作:

  1. 通过cc.Animation定义动画中基本信息:帧以及帧切换速度等
  2. 创建Action(cc.Animate)并在cc.Node上播放它


定义帧动画——cc.Animation

//定义cc.Animation
var animation = new cc.Animation();

//在这个例子里添加四帧动画,每一帧动画都是从res/my_pic.png这张图片的不同位置上截取的
animation.addSpriteFrame(new cc.SpriteFrame("res/my_pic.png", cc.rect(0, 0, 255, 476)));
animation.addSpriteFrame(new cc.SpriteFrame("res/my_pic.png", cc.rect(0, 480, 255, 476)));
animation.addSpriteFrame(new cc.SpriteFrame("res/my_pic.png", cc.rect(0, 960, 255, 466)));
animation.addSpriteFrame(new cc.SpriteFrame("res/my_pic.png", cc.rect(0, 1426, 255, 466)));

//定义每帧动画之间的时间间隔
animation.setDelayPerUnit(0.1);

播放动画

//使用前面创建的cc.Animation创建帧动画
var action = cc.RepeatForever.create(new cc.Animate(animation));

//在cc.Node节点上播放帧动画
someNode.runAction(action);



还可以使用cc.textureCache定义帧动画(适用于那种把所有帧定义在一张大图中的情况)

//使用textureCache管理大图
var texture = cc.textureCache.addImage("res/first_aganruan.png");

//创建cc.Animation,并通过texture加载帧图片内容
var animation = new cc.Animation();
animation.addSpriteFrameWithTexture(texture, cc.rect(0, 0, 255, 476));
animation.addSpriteFrameWithTexture(texture, cc.rect(0, 480, 255, 476));
animation.addSpriteFrameWithTexture(texture, cc.rect(0, 960, 255, 466));
animation.addSpriteFrameWithTexture(texture, cc.rect(0, 1426, 255, 466));


Cocos Creator编辑器中,可以通过给Animation添加动画事件来实现事件的执行。但是需要注意的是,如果想要事件成功回调,必须将事件监听的脚本放在与Animation组件同一个控件下。例如,在TestSprite组件上挂载了一个Animation脚本,在动画中添加了一个onAnimEnd事件,在Test脚本中实现了这个onAnimEnd函数。如果想要事件成功回调,必须将Test脚本挂载到TestSprite组件上,也就是要事件回调实现与Animation在同一个组件上。如果不想两个组件挂载到同一个地方,可以通过动态注册动画事件来实现。具体的方法可以参考Cocos Creator官方文档中的说明\[1\]。 除了在动画编辑器中添加帧事件进行回调外,Cocos Creator的动画系统还提供了动态注册回调事件的方式\[2\]。可以通过以下代码来注册和取消注册回调事件: ```javascript var animation = this.node.getComponent(cc.Animation); // 注册 animation.on('play', this.onPlay, this); // 取消注册 animation.off('play', this.onPlay, this); // 对单个cc.AnimationState注册回调 var anim1 = animation.getAnimationState('anim1'); anim1.on('lastframe', this.onLastFrame, this); ``` 此外,还可以动态创建Animation Clip,并添加帧事件\[2\]。具体的方法可以参考官方文档中的示例代码。 在使用动画播放接口时,可以通过指定动画名称、开始时间等参数来播放动画\[3\]。例如: ```javascript var anim = this.getComponent(cc.Animation); // 指定播放test动画 anim.play('test'); // 指定从1s开始播放test动画 anim.play('test', 1); // 使用play接口播放动画时,如果还有其他动画正在播放,则会先停止其他动画 anim.play('test2'); // 播放第一个动画 anim.playAdditive('position-anim'); // 播放第二个动画 anim.playAdditive('rotation-anim'); // 指定暂停test动画 anim.pause('test'); // 暂停所有动画 anim.pause(); // 指定恢复test动画 anim.resume('test'); // 恢复所有动画 anim.resume(); // 指定停止test动画 anim.stop('test'); // 停止所有动画 anim.stop(); ``` 还可以通过setCurrentTime方法设置动画的当前时间\[3\]。 希望以上信息能够帮助到您。如果还有其他问题,请随时提问。 #### 引用[.reference_title] - *1* [关于Cocos creator的Animation事件](https://blog.youkuaiyun.com/a641832648/article/details/78084315)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [CocosCreator 教你玩转Animation动画(第十四篇)](https://blog.youkuaiyun.com/qq_45021180/article/details/104718341)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值