官方文档里脚本里添加动画帧事件的代码,不触发

import { Animation, Component, _decorator } from 'cc';
const { ccclass, property } = _decorator;

@ccclass("MyScript")
class MyScript extends Component {    
    public start() {
        const animation = this.node.getComponent(Animation);
        if (animation && animation.defaultClip) {
            const { defaultClip } = animation;                        
            defaultClip.events = [
                {
                    frame: 0.5, // 第 0.5 秒时触发事件
                    func: 'onTriggered', // 事件触发时调用的函数名称
                    params: [ 0 ], // 向 `func` 传递的参数
                }
            ];                                         

            animation.clips = animation.clips;                        
        }
    }

    public onTriggered(arg: number) {
        console.log('I am triggered!', arg);
    }
}

难道必须要在动画组件的根节点再额外加一个脚本吗?不能在别的脚本里获取到animation组件注册帧事件么?

const animation = this.node.getComponent(Animation);
animation [‘onTriggered’] = this.onTriggered.bind(this);
这样试试行不行

2赞

大佬 牛:beer: