this is my code for a canvas game, my character is able to go left right and jump. The problem is that i want to make the character jump only once, then touch the ground again and now, to be able to jump again. In my code if i press w multiple times will jump forever.
const canvas = document.querySelector("canvas");
const c = canvas.getContext('2d')
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const gravity = 0.5;
let isGrounded;
class Player {
constructor() {
this.position = {
x: 100,
y: 100
}
this.width = 100
this.height = 100
this.velocity = {
x: 0,
y: 1
}
}
draw() {
c.fillStyle = 'black'
c.fillRect(this.position.x, this.position.y,
this.width, this.height)
}
update() {
this.draw();
this.position.x += this.velocity.x;
this.position.y += this.velocity.y;
if (this.position.y + this.height + this.velocity.y >= canvas.height)
this.velocity.y = 0
else this.velocity.y += gravity;
}
}
class Platform {
constructor({
x,
y
}) {
this.position = {
x: x,
y: y
}
this.width = 400
this.height = 20
}
draw() {
c.fillStyle = 'black'
c.fillRect(this.position.x, this.position.y,
this.width, this.height)
}
}
const player = new Player()
const platforms = [
new Platform({
x: 150,
y: 800,
}),
new Platform({
x: 700,
y: 900
}),
];
const keys = {
right: {
pressed: false
},
left: {
pressed: false
},
up: {
pressed: false
}
}
let scrollOffset = 0
function animate() {
const JUMP_SPEED = 5
requestAnimationFrame(animate)
c.clearRect(0, 0, canvas.width, canvas.height)
player.update();
platforms.forEach(platform => {
platform.draw()
})
if (keys.right.pressed && player.position.x < 400) {
player.velocity.x = 5
} else if (keys.left.pressed && player.position.x > 100) {
player.velocity.x = -5
} else {
player.velocity.x = 0
if (keys.right.pressed) {
scrollOffset += 5;
platforms.forEach((platform) => {
platform.position.x -= 5;
});
} else if (keys.left.pressed) {
scrollOffset -= 5
platforms.forEach((platform) => {
platform.position.x += 5;
});
}
}
//platform collision detection
platforms.forEach(platform => {
if (player.position.y + player.height <= platform.position.y &&
player.position.y + player.height + player.velocity.y >= platform.position.y &&
player.position.x + player.width >= platform.position.x &&
player.position.x <= platform.position.x + platform.width) {
player.velocity.y = 0
}
})
if (scrollOffset > 300) {
console.log('u win')
}
}
animate()
window.addEventListener('keydown', ({keyCode}) => {
switch (keyCode) {
case 65:
keys.left.pressed = true;
break;
case 83:
break;
case 68:
keys.right.pressed = true;
break;
case 87:
keys.up.pressed = true;
player.velocity.y = -10
break;
}
})
window.addEventListener("keyup", ({keyCode}) => {
switch (keyCode) {
case 65:
keys.left.pressed = false;
break;
case 83:
break;
case 68:
keys.right.pressed = false;
break;
case 87:
keys.up.pressed = false;
break;
}
});
<canvas></canvas>