Uncaught TypeError: this.entity.setLocalEulerAngles is not a function


#1

Hi, I am confused why I am getting this error:
Uncaught TypeError: this.entity.setLocalEulerAngles is not a function

I have used a setTimeout function while rotating a ‘bird’ entity, because I want to make it stay upright longer. Here is what I have done:

 var zrot = pc.math.clamp(this.velocity, -2, -0.75);
              zrot += 1;
              this.entity.setLocalEulerAngles(0, 0, zrot + 22.5);
            
              setTimeout(function () {
                  this.entity = zrot;
                 this.entity.setLocalEulerAngles(0, 0, zrot * 90);
             }, 750);

The bottom part of the code is where the error is occurring. However, I cannot solve the problem, because I don’t know what I can do to fix this. Any help? :smiley:

Game:

https://playcanvas.com/project/572229/overview/flappy-bird


#2

Because you are in a different function scope for the timeout callback, the this pointer is referencing a different object.

The easiest way to solve this is to create a local variable that can be captured for the anonymous function.

var self = this;
setTimeout(function () {
  self.entity.setLocalEulerAngles(0, 0, zrot * 90);
}

#3

Thank you, this is working now :grinning: