简体   繁体   English

如何将js onclick事件添加到框架动画

[英]How to add js onclick event to aframe animation

I have a spaceship model that I would like to animate taking off. 我有一个要模拟起飞的太空飞船模型。 I would like it to be so that the animation would activate once I click on the spaceship with my cursor. 我希望这样可以使动画在我用光标单击飞船后立即激活。 I have the animation working using but I would like it to activate upon a cursor click event. 我正在使用动画,但是我希望它在发生光标单击事件时激活。 Below is my HTML and where I got stuck with the JS 以下是我的HTML以及我在JS上遇到的问题

<a-entity id="spaceship" cursor-animator gltf-model="models/spaceship.gltf" position="-20 -0.855 -5.259" scale="2 2 2" rotation="0 180 0">
  <a-animation attribute="position" from="-20 0 -5" to="-20 0 -25" dur="10000"></a-animation>
  <a-animation attribute="position" from="-20 0 -25" to="-20 1000 -200" delay="9000" dur="9000"></a-animation>
</a-entity>


// Component to activate spaceship animation on click
AFRAME.registerComponent('cursor-animator', {
init: function () {
this.el.addEventListener('click', function (evt) {
    //Code
  console.log('I was clicked at: ', evt.detail.intersection.point);
});
}
});

I would try adding the 'begin' attribute to the animation tags: https://aframe.io/docs/0.8.0/core/animations.html#begin 我会尝试将'begin'属性添加到动画标签: https : //aframe.io/docs/0.8.0/core/animations.html#begin

<a-entity id="camera" camera look-controls cursor="rayOrigin: mouse"></a-entity>
<a-entity id="spaceship" cursor-animator gltf-model="models/spaceship.gltf" position="-20 -0.855 -5.259" scale="2 2 2" rotation="0 180 0">
  <a-animation begin="click" attribute="position" from="-20 0 -5" to="-20 0 -25" dur="10000"></a-animation>
  <a-animation begin="click" attribute="position" from="-20 0 -25" to="-20 1000 -200" delay="9000" dur="9000"></a-animation>
</a-entity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM