我这里有一些奇怪的行为,在渲染的 JS 中,update 方法中 this 的值是 window,但它应该是 Game(如在 onKeyDown 方法中)。你如何纠正这个?
class Game {
//for the key events
rightDown: bool = false;
constructor() {
}
onKeyDown(evt) {
if (evt.keyCode == 39) this.rightDown = true;
else if (evt.keyCode == 37) this.leftDown = true;
if (evt.keyCode == 32) {
this.space = true;
var bullet = new GameObjects.GameObjects.Bullet(10);
this.addProjectile(bullet);
};
}
update(elapsed: number) {
if (this.rightDown) {
console.log(this.rightDown);
}
}