AFrame问题,其中setAttribute重置以前的属性

时间:2017-05-04 16:17:09

标签: three.js aframe

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

我创建了一个propriety types,您可以在其中看到行为。

现在,如果我致电position更新target,则会更改位置,但1 2 3 4 6 7 8 20 24 28 32 适当性会重置为默认值。此外,实体属性保持不变。那是为什么?

2 个答案:

答案 0 :(得分:0)

为A-Frame原语重写方法setAttribute。在调用方法将组件作为属性附加到已存在的节点时,它会覆盖现有的组件属性。因此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方法有不同的实现。

答案 1 :(得分:0)

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

尝试:

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

或者:

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