动画剪辑结束后,如何使用Javascript触发Aframe动画混合器事件?

时间:2018-12-30 13:32:27

标签: javascript aframe

我最初是使用don mccurdy的animation-mixer在.fbx模型上播放动画的。我要实现的目标是,只要当前播放的动画结束,就会触发一个事件,该事件将使“再次播放”按钮可见。当我单击此按钮时,动画将再次开始播放。

但是事件按钮在动画开始播放后立即可见,而不是在事件“动画结束”触发时可见。

代码:

<html>
    <head>
        <meta charset="utf-8">
        <script src="https://aframe.io/releases/0.8.0/aframe.min.js"></script>
        <script src="https://rawgit.com/donmccurdy/aframe-extras/master/dist/aframe-extras.loaders.min.js"></script>
        <script src="inflate.min.js"></script>
    </head>
    <body>
        <a-scene>
        <a-sky color="#6EBAA7"></a-sky> 
            <a-entity id="animationPlayer" scale="0.012 0.012 0.012" fbx-model="src: kick.fbx" animation-mixer="clip: *;loop: repeat; repetitions: 2;"></a-entity>
            <a-entity position="0 1.5 3.5"><a-camera></a-camera></a-entity>
            <a-box id="play_again" position="0 3 -1" height=0.5 depth=0.09 visible="false" color="red"></a-box>
            <a-text value="Play again" position="-0.53 3 -0.95"></a-text>
        </a-scene>
        <script>
            document.querySelector("#animationPlayer").addEventListener('animation-finished',function(){
                document.querySelector("#play_again").setAttribute('visible', 'true')
            });
        </script>
    </body>
</html>
  • 预期结果: 仅当动画播放完毕时,该按钮才可见。

  • 实际结果: 当动画仍在播放时,该按钮已经可见。

那么,我在语法上缺少什么吗?还是有逻辑上的问题?

1 个答案:

答案 0 :(得分:0)

我从问题中假设以下内容 单击该按钮时,该按钮可见,并且该模型具有动画效果,并且该按钮是隐藏的。动画完成后,返回按钮并重置动画。

let button = document.querySelector("#play_again");
let player = document.querySelector("#animationPlayer");
button.addEventListener('click',function(){
  //hide the button
  button.setAttribute('visible','false');

  //add the event listener to handle post animation before starting animations 
  (use once to help with cleanup)
  player.addEventListener('animation-finished',function() {
    button.setAttribute('visible','true');
    player.removeAttribute('animation-mixer');
  },{once:true});

  //start the animation by appending the animation-mixer
  player.addAttribute('animation-mixer',{clip: "*",loop: "repeat", repetitions: 2});
});

从开始的html中删除animation = -mixer

<a-entity id="animationPlayer" scale="0.012 0.012 0.012" fbx-model="src: kick.fbx"></a-entity>