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");
      });
    });
  }

}

我正在创建一个简单的对撞机。我将跟踪具有“对撞机”组件的元素,并使用intersectsBox. 不幸的是,我似乎无法让 MutationObserver 工作。我宁愿使用这种方法而不是刻度线,因为它会在每帧开始执行它,而不是在元素移动时。

有什么建议么?

4

1 回答 1

1

您可以使用

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

  }
});

但是通过 tick 进行轮询/滴答是同步的,并且可能仍然不是一个糟糕的方法。

于 2017-07-21T22:25:01.013 回答