如何在动画剪辑结束时使用 Javascript 触发 Aframe 动画混合器事件?
How to fire an Aframe animation-mixer event using Javascript when an animation-clip ends?
我最初使用 don mccurdy 的动画混合器在 .fbx 模型上播放动画。我想要实现的是,只要当前播放的动画结束,就会触发一个事件,使 'Play again' 按钮可见。当我点击这个按钮时,动画又开始播放了。
但是事件按钮在动画开始播放后立即可见,而不是在事件 'animation-finished' 触发后可见。
代码:
<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>
预期结果:
该按钮仅在动画播放完毕后可见。
实际结果:
当动画还在播放时,按钮已经可见。
所以,我是在语法上遗漏了什么还是存在逻辑问题?
我假设问题如下
按钮是可见的,模型是动画的,单击按钮时按钮是隐藏的。当动画完成时,返回按钮并重置动画。
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>
我最初使用 don mccurdy 的动画混合器在 .fbx 模型上播放动画。我想要实现的是,只要当前播放的动画结束,就会触发一个事件,使 'Play again' 按钮可见。当我点击这个按钮时,动画又开始播放了。
但是事件按钮在动画开始播放后立即可见,而不是在事件 'animation-finished' 触发后可见。
代码:
<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>
预期结果: 该按钮仅在动画播放完毕后可见。
实际结果: 当动画还在播放时,按钮已经可见。
所以,我是在语法上遗漏了什么还是存在逻辑问题?
我假设问题如下 按钮是可见的,模型是动画的,单击按钮时按钮是隐藏的。当动画完成时,返回按钮并重置动画。
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>