【问题标题】:How to start an animation on an object on clicking another object in a-frame?单击框架中的另一个对象时,如何在对象上启动动画?
【发布时间】:2018-01-17 06:27:45
【问题描述】:

我正在使用 a-frame 在 WebVR 上制作一个项目,其中我加载了横幅和 a-box 元素的 collada 模型。单击模型时,我想围绕其轴旋转框。我使用了 javascript,但不是动画,而是直接旋转框,这与我们使用 a-frame 的动画标签时发生的情况非常不同。

<script>

    function changeView() {
        var view = document.getElementById("float");
        view.setAttribute("rotation",
        {
            x: 0,
            y: 360,
            z: 0    		
        });
    }
</script>
<a-scene>
  
  <a-assets>
    <a-asset-item id="floatingbanner" src="models/floatingbanner.dae"></a-asset-item>
  </a-assets>
  
  <a-entity id="float" collada-model="#floatingbanner" position="-2 2 0" scale="0.3 0.3 0.3" onclick="loaddoc()">
  </a-entity>

  <a-box id="box" position="-1 1.5 0" onclick="changeView()" height=".3" depth=".3" width=".3"></a-box>
 
  <a-camera id="view" position="0 0 0">
          <a-cursor color="black"/>
  </a-camera>

<a-scene>

【问题讨论】:

    标签: javascript html three.js aframe webvr


    【解决方案1】:

    您需要使用&lt;a-animation&gt;&lt;/a-animation&gt;

    定义动画:

    <a-entity>
      <a-entity position="5 0 0"></a-entity>
      <a-animation attribute="rotation"
                   dur="10000"
                   fill="forwards"
                   to="0 360 0"
                   begin="startAnimation"
                   repeat="indefinite"></a-animation>
    </a-entity>
    

    然后emit()begin 事件像这样:

    function changeView() {
        var view = document.getElementById("float");
        view.emit("startAnimation");
    }
    


    还尝试通过将脚本附加到您的实体来使用组件系统:
    AFRAME.registerComponent('foo',{
      init:function(){
      var view = document.getElementById("float");
         this.el.addEventListener('click',function(){
             view.emit("startAnimation");
         });
      }
    });
    

    【讨论】:

    • 使用上述方法,似乎有道理,但无法实现:jsfiddle.net/RonK/hqrk4e5w/33。有什么想法有什么问题吗?将每个设置为通过“点击”进行旋转对它们自己来说效果很好。我正在尝试让每个人激活另一个人。
    猜你喜欢
    • 2023-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 2015-04-21
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多