a.barbieri
a.barbieri

Reputation: 2596

Issue with AFrame where setAttribute reset previous proprieties

I registered an AFRAME component called mycomponent which simply points the camera towards a target and changes position on click. To do that the component has two propriety types which are target and position.

I created a pen where you can see the behaviour.

Now if I call setAttribute to update the position, it changes position but the target propriety is reset to default. Furthermore the entity attribute remains the same. Why is that?

Upvotes: 0

Views: 525

Answers (2)

ngokevin
ngokevin

Reputation: 13233

That may be a bug if you pass a string form into .setAttribute.

Try:

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

Or:

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

Upvotes: 0

Shane
Shane

Reputation: 3199

The method setAttribute is overridden for A-Frame primitives. When calling the method to append a component as attribute to the node which already exists, it overrides the existing component attribute. Thus target is reset. Seeing you are using an AEntity, you may set an attribute of a component to a certain value. Check the js docs of the setAttribute method in aframe/core/a-entity.js

To solve the problem, simply call:

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

Or...

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

...which gives you the option to use (custom) delimiters (default is ".") for the first parameter as wel AFRAME.utils.entity.setComponentProperty(camera, 'mycomponent.position', '-10 0 10' [, delimiter]).

** Important to note ANode, which is inherited by AEntity, has a different implementation for the setAttribute method.

Upvotes: 1

Related Questions