How to start an a-frame animation by an “animationend” javascript function?

柔情痞子 提交于 2019-12-11 08:26:13

问题


I never worked with html, javascript or a-frame and I want to make three collada files visible and then invisible one after another (like a keyframe animation sequence)and loop that animation. That code works except for the possibility to loop the animation.

<script src="https://aframe.io/releases/0.7.1/aframe.min.js"></script>
<script src="https://rawgit.com/donmccurdy/aframe-extras/master/dist/aframe- 
extras.loaders.min.js"></script>
<script src="play-all-model-animations.js"></script>

<body>

<a-scene>

<a-assets>
<a-asset-item id="t1" src="1a.gltf"></a-asset-item>
<a-asset-item id="t2" src="2a.gltf"></a-asset-item>
<a-asset-item id="t3" src="3a.gltf"></a-asset-item>     
</a-assets>


<a-gltf-model id="model1" src="#t1" visible="false">
<a-animation id="anim1" attribute="visible" begin="1000"; dur="2000"; 
 to="false">
</a-animation>  
</a-gltf-model>

<a-gltf-model id="model2" src="#t2" visible="false">
<a-animation id="anim2"   attribute="visible" begin="2000"; dur="2000"; 
to="false">
</a-animation> 
</a-gltf-model>

<a-gltf-model id="model3" src="#t3" visible="false">
<a-animation id="anim3" attribute="visible" begin="3000";  dur="2000"; 
to="false">
</a-animation> 
</a-gltf-model>


<script>
anim1.addEventListener('animationend', function () {
model1.setAttribute('visible', 'true');
});
</script>

<script>
anim2.addEventListener('animationend', function () {
model1.setAttribute('visible', 'false');
model2.setAttribute('visible', 'true');
});
</script>

<script>
anim3.addEventListener('animationend', function () {
model2.setAttribute('visible', 'false');
model3.setAttribute('visible', 'true');

});
</script>

</a-scene>

</body>

I think I'll have to to start each animation in the "animationend function" instead of starting the animation with a "begin" time. Does anybody have an idea how to start e.g. the animation id="anim1" by a javascript function? I would appriciate any kind of help.


回答1:


You need to listen for the animationend event via animation.addEventListener('animationend', function)

and fire the begin event by element.emit('begin') considering such setup:

<a-element id="element">
   <a-animation id="animation"></a-animation>
</a-element>

check out my fiddle



来源:https://stackoverflow.com/questions/49575681/how-to-start-an-a-frame-animation-by-an-animationend-javascript-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!