CocosCreator开发笔记(18)-如何处理Animation动画的结束回调

CocosCreator目前支持的Animation回调事件有:

  • play : 开始播放时
  • stop : 停止播放时
  • pause : 暂停播放时
  • resume : 恢复播放时
  • lastframe : 假如动画循环次数大于 1,当动画播放到最后一帧时
  • finished : 动画播放完成时

首先确保节点已经添加了cc.Animation组件,并且至少添加了一个Animation Clip,假设该clip名称为’run’。

在 onStart 获取Animation对象并保存起来

onStart() {
    this.animCtrl = this.node.getComponent(cc.Animation);
}

在需要处播放动画

this.animCtrl.play('run');

注册回调有2种方法,一种是对 cc.AnimationState 注册回调,如下:

// 对单个 cc.AnimationState 注册回调
var animState = this.animCtrl.getAnimationState('run');
if (animState) {
    animState.on('stop', (event) => {
        // 处理停止播放时的逻辑
        let clip = event.detail;
        ...
    }, this);
}

另外一种是对 cc.Animation 注册回调,如下:

// 注册播放动画结束的回调
this.animCtrl.on('stop', this.onAnimStop, this);

onAnimStop回调函数的实现,用来动画停止播放时的处理

onAnimStop: function(event) {
    let animState = event.detail;
    if (animState.name === 'run' && event.type === 'stop') {
        // 注销回调函数
        this.animCtrl.off('stop', this.onAnimStop, this);
        ...
    }
}

这2种注册回调方法的区别是,对 cc.AnimationState 注册回调,它仅会在该 clip 发生相应事件时进入回调,方便对不同 clip 做不同处理。而对 cc.Animation 注册回调,则会在每个 clip 发生相应事件时都会进入回调,方便做全局处理。一般情况下,我们使用第一种,对 cc.AnimationState 注册回调就可以了。

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 creatorAnimation事件](https://blog.csdn.net/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.csdn.net/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 ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值