I am making this game in TypeScript, and trying to add and remove a "keydown" event. The event function is referring to "this", witch is the document, but i am trying to refer to the object instance.
How can this be achieved?
class Player {
board: Board = new Board();
pice: Pice = new Pice();
constructor() {
this.addKeyEvents();
}
addKeyEvents(): void {
document.addEventListener("keydown", this.keyClick);
}
removeKeyEvents(): void {
document.removeEventListener("keydown", this.keyClick);
}
keyClick(event: KeyboardEvent): void {
console.log(this); // #document
switch (event.keyCode) {
// Left
case 37:
this.pice.move(-1, 0, this.board);
break;
}
}
}