2017-04-13 52 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>