Aframe 如何通过实体'正确添加和删除光标组件;什么是设置属性?
根据ngokevin在上一个问题中提出的方法上的有益改变,我现在尝试在两个实体(每个实体都是单独相机的子实体)之间交换光标组件。然而,我看到了两种我没有预料到的行为:Aframe 如何通过实体'正确添加和删除光标组件;什么是设置属性?,aframe,Aframe,根据ngokevin在上一个问题中提出的方法上的有益改变,我现在尝试在两个实体(每个实体都是单独相机的子实体)之间交换光标组件。然而,我看到了两种我没有预料到的行为: 当我调用entityEl.removeAttribute(“游标”)时,它会删除游标组件,但会留下隐式添加的raycaster组件。(演示:) 当我调用entityEl.setAttribute('cursor')时,我不会在inspector中观察到对实体的任何添加。(演示:) 在这里,我可以验证在第一个案例解决该问题后,是
- 当我调用entityEl.removeAttribute(“游标”)时,它会删除游标组件,但会留下隐式添加的raycaster组件。(演示:)
- 当我调用entityEl.setAttribute('cursor')时,我不会在inspector中观察到对实体的任何添加。(演示:)
AFRAME.registerComponent( 'add-cursor-on-click', {
init: function() {
this.el.setAttribute('cursor');
this.el.addEventListener( 'click', function() { console.log("Received click ev."); } );
}
});
<a-entity id="onlyTheCursorAfterClick" add-cursor-on-click></a-entity>
AFRAME.registerComponent('add cursor on click'{
init:function(){
this.el.setAttribute('cursor');
this.el.addEventListener('click',function(){console.log(“Received click ev”);});
}
});
如往常一样,任何见解都将不胜感激。光标组件的删除生命周期处理程序似乎是最近添加的,因此它当前只能在主机上正确运行,请参阅此拉动请求: 游标组件也不会将事件直接附加到其init上。它等待
加载渲染目标
()
因此,在组件的init中,如果要删除游标组件,则在组件的init中需要:
if (!canvas) {
this.el.sceneEl.addEventListener('render-target-loaded', this.init.bind(this));
return;
}
但是,正如您所看到的,您仍然需要手动移除光线投射器
this.el.removeAttribute('raycaster');
要添加光标
组件,您需要传递组件的数据
this.el.setAttribute('cursor', {});
原因是存在未定义的