I'm trying to create a simple space shooter game using typescript and i've got kind of stuck at checking the movement.The thing is that if i only press one key it works fine but if i press 2 keys at the same time it won't check for collisions with margins.Do i have to check for every case if i press multiple buttons or there's another way to do it? Here's the code:
update(canvas) {
if (Game.keysPressed[KeyBindings.LEFT]) {
this.xVel = -1;
if (this.x <= 20) {
this.xVel = 0
}
} else if (Game.keysPressed[KeyBindings.RIGHT]) {
this.xVel = 1;
if (this.x + this.width >= canvas.width - 20) {
this.xVel = 0;
}
} else if (Game.keysPressed[KeyBindings.UP]) {
this.yVel = -1;
if (this.y + this.height <= 500) {
this.yVel = 0;
}
} else if (Game.keysPressed[KeyBindings.DOWN]) {
this.yVel = 1;
if (this.y + this.height >= 880) {
this.yVel = 0;
}
}
else {
this.xVel = 0;
this.yVel = 0;
}
this.x += this.xVel * this.speed;
this.y += this.yVel * this.speed;
}
Thanks for helping!