I want to make the player jump higher when holding space for a long time and only a small jump when pressing space for a short time. I'm having trouble increasing the airSpeed because jump() is only called once at the start of a jump and I'm not sure how to increase the airSpeed for as long as holding space but not above the maximum jump height. Here is my current implementation of jumping:
private void mainLoop() {
if (jumping) {
jumpTime++;
if (canJump) {
// the jump method is only called once when pressing space.
jump();
}
}
}
// ====== Jumping ======
private boolean canJump = true;
private boolean jumping = false;
private static final float MAX_JUMP_HEIGHT = 8.0f * SCALE;
private static final float MIN_JUMP_HEIGHT = 4.0f * SCALE;
private float jumpHeight = MIN_JUMP_HEIGHT;
private float jumpTime = 0.0f;
public void jump() {
if (inAir || !canJump) {
return;
}
inAir = true;
canJump = false;
// Here, I need to set airSpeed depending on how long the player has pressed space
airSpeed -= jumpHeight;
}
And here is where I set jumping to true when pressing space and to false when releasing space.
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
player.setJumping(true);
player.setJumpTime(0);
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_SPACE) {
player.setJumping(false);
player.setCanJump(true);
}
}
Any help is much appreciated!