0

I want to remove a piece of code inside a .tick handler in AFRAME using js.

This is what I have:

AFRAME.registerComponent('foo', {
 schema: {},
 init: function () {},
 update: function () {},
 tick: function() {
        myObject.position.y += 0.009;//this is what I want to remove on the fly
 },
remove: function () {},
pause: function () {},
play: function () {}
});

I expect to trigger the add or removal of that piece of code on the fly using js.

3 Answers 3

2

Sorround your logic with an if statement that checks for the appropriate conditions:

tick: function() {
  if (conditions) {
     myObject.position.y += 0.009;
  }
}

You can set those conditions anywhere in your code: component local or global variable, component property...

0

Thanks! I used the conditional like this:

 AFRAME.registerComponent('foo', {
 schema: {},
 init: function () {
  condition = false;
    },
 update: function () {},
 tick: function() {
    if (condition == true) {
    myObject.position.y += 0.009;//this is what I want to remove on the fly
      }
 },
remove: function () {},
pause: function () {},
play: function () {}
});

Now I can trigger the state with:

    condition = false;
1
  • 3
    Good job! Another idea is adding the condition as part of the schema to avoid the global variable: schema: {enabled: { default: false }} and you can do el.setAttribute(‘foo’, {enabled: false}) Dec 29, 2018 at 19:04
0

I think you misunderstood the point of schema and updates, but you are really really close with your logic.

I would add the condition into the schema so that you can use it anywhere within your component as well as use AFrame sugar to update it using setAttribute.

AFRAME.registerComponent('foo', {
  schema: {
    condition: {type: boolean, default:false}
  },
  init: function () {
    //possible once off initialization logic
  },
  update: function () {},
  tick: function() {
    if (this.data.codition) {
      myObject.position.y += 0.009;//this is what I want to remove on the fly
    }
  },
  remove: function () {},
  pause: function () {},
  play: function () {}
});

Then on the fly you simply do

yourElementWithFooComponent.setAttribute("condition":"true"/"false");

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.