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 工作。我宁愿使用这种方法而不是刻度,因为它将开始每帧执行它,而不是当元素移动时。
有什么建议吗?
最佳答案
你可以使用
el.addEventListener('componentchanged', function (evt) {
if (evt.detail.name === 'position') {
}
});
但是通过滴答进行轮询/滴答是同步的,并且可能仍然不是一个坏方法。
关于javascript - 是否可以将 Mutation Observer 与三个 js 元素 - A 框架一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45246216/