How to increase jump height when holding space

I want the jump height to be depending on how long the player holds space. My question is how I can modify the airSpeed depending on the jumpTime (the time that player has held down space). Currently it's not adjusting jumpTime properly and I don't see how I can fix it. Here is my current implementation:
private void mainLoop() {
if (jumping) {
jumpTime++;
if (inAir)
return;
inAir = true;
canJump = false;

// need to set airspeed depending on jump time here
airSpeed -= jumpHeight;
}
}
}

// ====== Jumping ======
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 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);
}
}
private void mainLoop() {
if (jumping) {
jumpTime++;
if (inAir)
return;
inAir = true;
canJump = false;

// need to set airspeed depending on jump time here
airSpeed -= jumpHeight;
}
}
}

// ====== Jumping ======
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 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);
}
}
1 Reply
JavaBot
JavaBot2y ago
This post has been reserved for your question.
Hey @Erky! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here. 💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.

Did you find this page helpful?