Get 50% off all memberships! Black Friday offer ends Dec. 6 @11:59pm EST. View Pricing
You can add a basic condition that checks if your player is currently moving on the y axis, like:
if (player.velocity.y == 0) player.velocity.y = -10;
You can extend this further by adding a falling state, as to say, if by the odd chance your character's y velocity is 0
when they've reached the peak of the jump, you don't want to be able to reset the player's y velocity back to -10 (this would cause more jump spam).
So what you could do is within your event listener:
if (player.velocity.y !== 0 && !player.travelingUpwards) {
player.velocity.y = -10
player.travelingUpwards = true
}
Then for every frame within your animation loop, check if the player is falling down:
if (player.velocity.y > 0) {
player.travelingUpwards = false
}
Combined together, you should prevent jump spam and prevent a player from jumping if they reach the peak and their y velocity is 0
.
Want to participate?
Create a free Chris Courses account to begin