2017-07-21 56 views
0
export class ColliderComponent { 

    constructor() { 
    this.observer = this.mutationObserver();  
    this.aframe(); 
    } 


    //Registers the AFRAME component. 
    aframe(){ 
    const __this = this; 
    AFRAME.registerComponent('collider', { 
     schema: { 

     }, 
     init: function() { 
      console.log("The element to be observed is:",this.el); 

      __this.observer.observe(this.el, {characterData:true, subtree:true, attributes: true, attributeFilter: ['position'], childList : true}); 
     }, 

     tick : function(){ 
     console.log(this.el.getObject3D('mesh').position); 
     } 
    }); 

    } 

    private tick(){ 

    } 

    private mutationObserver() : MutationObserver{ 
    return new MutationObserver(mutations => { 
     mutations.forEach(mutation => { 
     console.log("Changed position"); 
     }); 
    }); 
    } 

} 

我正在創建一個簡單的碰撞器。我將跟蹤具有「collider」組件的元素,並使用intersectsBox來檢查它們是否相交。不幸的是,我似乎無法讓MutationObserver工作。我寧願使用這種方法而不是滴答聲,因爲它會在每幀中開始執行,而不是在元素移動時執行。是否可以使用具有三個js元素的突變觀察者 - A-Frame?

有什麼建議嗎?

+0

我也試着用getObject3D('mesh')檢查位置。但是,當我使用A-Frame的動畫組件時,這不會觸發任何事情。 –

回答

1

您可以使用

el.addEventListener('componentchanged', function (evt) { 
    if (evt.detail.name === 'position') { 

    } 
}); 

但輪詢/剔通過滴答作響的是同步的,可能仍然不是一個壞的路要走。

+0

ooof。傳說本身回答我。我會給它一個鏡頭,然後回覆。謝謝! –

+0

傳說自己* –

+0

太棒了。一切都很好。我將使用該組件,一旦完成,我將在Github中發佈它。 –

相關問題