Fix movement of tank

This commit is contained in:
Nikita Iryupin 2021-04-22 10:22:28 +02:00
parent 1cda01992f
commit 66b9bc713c
2 changed files with 19 additions and 3 deletions

View File

@ -8,7 +8,23 @@ export class Game {
}
start() {
addEventListener("keydown", (ev) => this.gameKeyAdapter.handleEvent(ev));
let prevEv;
let holding = (ev) => {
if (!prevEv || prevEv.key != ev.key) return;
this.gameKeyAdapter.handleEvent(ev)
requestAnimationFrame(() => holding(ev));
}
addEventListener("keydown", (ev) => {
if (prevEv) {
if (prevEv.key != ev.key) prevEv = undefined;
else return;
}
prevEv = ev;
requestAnimationFrame(() => holding(ev));
});
addEventListener("keyup", (ev) => {
if (prevEv && prevEv.key == ev.key) prevEv = undefined;
});
requestAnimationFrame(() => this.gameField.drawField());
}
}

View File

@ -29,7 +29,7 @@ export class PlayerTank {
}
this.direction = direction;
console.log(this.x + " " + this.y);
// console.log(this.x + " " + this.y);
}
draw(tileset, ctx, tilesize) {