2017-04-13 91 views
1

我想在我的Aframe应用程序中获取摄像头的位置,但它始终为0,尽管我移动了摄像头。获取摄像头位置javascript

我试了一个事件监听器和一个常规的位置检索,但没有得到更新的值。

这里是现场:

<a-scene> 
    <a-entity id="cameraWrapper" position="0 2 10" rotation="0 0 0"> 
    <a-camera near="0.1" user-height="0" id="camera"></a-camera> 
    </a-entity> 
    <a-plane position="0 4 4" rotation="-90 0 -90" width="4" height="4" color="#7BC8A4"></a-plane> 
    <a-sky color="#ECECEC"></a-sky> 
</a-scene> 

这里是监控摄像头的位置的JavaScript代码:

<script> 
    window.onload = function() { 
     var pos = 
    document.querySelector('#camera').getAttribute('position'); 
    document.querySelector('[camera]').addEventListener('componentchanged', function (evt) { 
     console.log('CAMERA x/y:', pos.x, pos.y); 
     if (evt.name === 'position') { 
      console.log('Entity has moved from POSITION', evt.oldData, 'to', evt.newData, '!'); 
      return; 
     } 
      return; 
     }); 
    }; 
    </script> 

但条件如果(evt.name ===“位置')永远不会是真的,并且相机位置仍然为0,即使在用键盘输入移动相机位置时也是如此。我怎样才能连续获取相机位置?

回答

4

我认为这是evt.detail.name === 'position'

我还建议使用一个A型架组件,而不是松散的JavaScript:

AFRAME.registerComponent('listener', { 
    tick: function() { 
    console.log(this.el.getAttribute('position')); 
    } 
}); 

HTML:

<a-camera listener></a-camera>