I have a class that I define with a constructor like this:
var Snake = function(x, y) {
this.MoveStates = {'UP': 0, 'DOWN': 1, 'LEFT': 2, 'RIGHT': 3};
this.moveState = this.MoveStates.DOWN;
window.onkeydown = this.handleInput;
};
In the handleInput method I want to be able to access the move state variables, but I'm having some trouble doing so. The method looks like this:
Snake.prototype.handleInput = function(event) {
switch (event.keyCode) {
case 87: //W
this.moveState = this.MoveStates.UP;
break;
case 65: //A
this.moveState = this.MoveStates.LEFT;
break;
case 83: //S
this.moveState = this.MoveStates.DOWN;
break;
case 68: //D
this.moveState = this.MoveStates.RIGHT;
break;
};
};
In this method the keyword this references the window object that I bound the event to, if I'm not mistaken. How can I obtain a reference to the snake object?
window.onkeydown = this.handleInput.bind(this);