CocosCreator开发笔记(18)-处理Animation动画回调

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/foupwang/article/details/81517667

首先确保节点里已经添加了cc.Animation组件,并且至少添加了一个名为’run’的Animation Clip。
在 onStart 获取Animation对象并保存起来

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

在需要处播放动画

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

回调函数实现,用来播放动画结束的处理

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

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

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

猜你喜欢

转载自blog.csdn.net/foupwang/article/details/81517667