This really depends on how you want to implement the mechanic. For me, a common "double jump" would be a second jump in mid air, if you're still on your way up. As such, I'd probably do something like this:
// dY is my vertical velocity; positive is pointing upwards
if (keyTapped(JUMP)) {
if (onGround()) {
dY = 10;
doubleJumped = false;
}
else if (dY > 0 && !doubleJumped) {
dY = 10; // the second jump
doubleJumped = true;
}
}
Another idea would be an intuitive jump height based on how long ("hard") you press the jump key:
// jumpFrames is a counter to count the number of logic steps after starting the jump
if (keyDown(JUMP)) {
if (onGround()) {
jumpFrames = 0;
dy = 5;
}
else if (jumpFrames++ < 5) {
dY += 1;
}
}
Both snippets are untested, but should work:
- The first one allows you to jump and then jump again while you're still ascending.
- The second one allows you to jump higher if you hold down the button for longer.
Both snippets assume there's some kind of friction/gravity slowing your velocity down again. The actual values might depend on your physics model.