A Frame 的问题,其中 setAttribute 重置了以前的属性

Issue with AFrame where setAttribute reset previous proprieties

我注册了一个名为 mycomponent 的 AFRAME 组件,它只是将相机指向目标并在单击时更改位置。为此,该组件有两个 propriety types,即 targetposition

我创建了一个 pen,您可以在其中查看行为。

现在,如果我调用 setAttribute 来更新 position,它会改变位置,但 target 属性会重置为默认值。此外,实体属性保持不变。这是为什么?

方法 setAttribute 被 A 帧基元覆盖。当调用方法将组件作为属性附加到已存在的节点时,它会覆盖现有的组件属性。因此 target 被重置。看你用的是AEntity,你可以把一个组件的属性设置成某个值。 查看aframe/core/a-entity.js

中setAttribute方法的js文档

要解决问题,只需调用:

let camera = document.getElementById('camera')
  camera.setAttribute('mycomponent', 'position', '-10 0 10')

或者...

let camera = document.getElementById('camera')
  AFRAME.utils.entity.setComponentProperty(camera, 'mycomponent', 'position', '-10 0 10')

...这让您可以选择为第一个参数使用(自定义)定界符(默认为“.”)以及 AFRAME.utils.entity.setComponentProperty(camera, 'mycomponent.position', '-10 0 10' [, delimiter])

** 需要注意的是,AEntity 继承的 ANode 对 setAttribute 方法有不同的实现。

如果您将字符串形式传递给 .setAttribute,这可能是一个错误。

尝试:

el.setAttribute('component', {position: '-10 10 10'});

或者:

el.setAttribute('component', 'position', '-10 10 10');