以下是改写后的代码:

// 获取动画节点 var animNode = cc.find("animNode");

// 绑定点击事件 animNode.on(cc.Node.EventType.TOUCH_END, function () { // 播放惊吓动画 var scaredAnim = animNode.getComponent(cc.Animation).play("scared");

// 监听惊吓动画播放完成事件
scaredAnim.on("finished", function () {
    // 判断当前动画状态
    var animState = animNode.getComponent(cc.Animation).getAnimationState("scared");
    if (animState.isPlaying) {
        // 如果当前为惊吓状态,切换到休息动画
        animNode.getComponent(cc.Animation).play("rest");
    } else {
        // 如果当前为休息状态,切换到站立动画
        animNode.getComponent(cc.Animation).play("stand");
    }
});

});

注:该代码假定动画节点上已经添加了 Animation 组件,并且动画状态名称为 "scared"、"rest"、"stand"。同时,需要注意动画节点的触摸事件类型是否为 TOUCH_END


原文地址: https://www.cveoy.top/t/topic/fljm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录