My current project involves creating an avatar that follows the movement of my mouse.
After successfully developing a functional avatar, I'm now looking to replicate it using an object constructor. The only difference is that instead of var angleToBe;
, I use this.angleToBe;
. Everything is directly linked to the constructor, but I encountered a problem when constructing the methods, particularly the turnToMouse
function. Here's how my syntax looks:
Avatar.prototype.turnToMouse = function() {
if (this.canTurn) {
this.spotX = this.body.position.x;
this.spotY = this.body.position.y;
this.spotY = this.spotY * -1 + height;
this.body.angleToBe = Math.atan2(cursorX - this.spotX, cursorY - this.spotY);
this.body.__dirtyRotation = true;
this.body.__dirtyPosition = true;
this.gun.__dirtyRotation = true;
this.gun.__dirtyPosition = true;
this.body.rotation.set(0, 0, avatarAngleToBe);
}
setTimeout(this.turnToMouse, time);
};
The constructor used is for Avatar.
This function continuously adjusts the position of the avatar and its gun towards the direction of the mouse. It should work without issues since it mirrors my existing working function perfectly, except for the added this
references.
The trouble arises when I include this code within the constructor (function Avatar() {}
):
this.turnToMouse();
Here, the editor fails to recognize the function 'turnToMouse()
' despite its existence.
Can anyone spot what might be missing in this setup?
PS: If you'd like to view the complete code with the operational avatar and the object avatar, simply click on this link using Google Chrome.