美文网首页
CocosCreator-如何处理Animation动画的结束回

CocosCreator-如何处理Animation动画的结束回

作者: 程序猿TODO | 来源:发表于2021-03-24 10:03 被阅读0次

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注册回调就可以了。

相关文章

  • CocosCreator-如何处理Animation动画的结束回

    CocosCreator目前支持的Animation回调事件有: play : 开始播放时 stop : 停止播放...

  • CocosCreator-如何处理Spine动画的结束回调

    CocosCreator提供了很方便的Spine动画接口,我们常常会在播放Spine动画完成后做一些处理,这就需要...

  • iOS动画- Core Animation

    Core Animation译为核心动画,它是一组非常强大的动画处理API。Core Animation的动画执行...

  • iOS动画

    1、Core Animation Core Animation,即为核心动画,它是一组非常强大的动画处理API,使...

  • iOS图层配合核心动画详解

    Core Animation Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,...

  • 层动画-可做动画的属性!

    图层动画很像视图动画;你只需在某段时间内,在开始和结束值之间对属性进行动画处理,然后让Core Animation...

  • 核心动画(一)

    一、Core Animation简介 Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理...

  • Core Animation

    一:Core Animation简介 Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理...

  • Core Animation

    Core Animation Core Animation 也就是所谓的核心动画,是一组非常强大动画处理API,可...

  • Core Animation 核心动画

    Core Animation 核心动画 Core Animation是一组非常强大的动画处理API,使用它能做出非...

网友评论

      本文标题:CocosCreator-如何处理Animation动画的结束回

      本文链接:https://www.haomeiwen.com/subject/yuwxhltx.html