0

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!

Bart
  • 59
  • 5
  • 1
    We need to see more code. For example, we should where and how the key listeners are registered. Please provide a [minimal, complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example) – Old Dog Programmer Apr 16 '23 at 16:36
  • @OldDogProgrammer There's a lot of code for jumping and falling, just figured I would put the most relevant information down. Just want to get some ideas on how I can implement this, I'm having trouble updating jumpHeight depending on how long space is being pressed. KeyListeners are registered by implementing KeyAdapter and added to the JPanel? If that was your question – Bart Apr 16 '23 at 17:05
  • Have you tried retrieving the system time when the key is pressed and again when the key is released? The jump strength would be based on the time difference, perhaps in microseconds or nanoseconds. – Old Dog Programmer Apr 16 '23 at 17:25
  • @OldDogProgrammer yes, problem is how I adjust the jump strength or the jump functionality. I want the player to keep increasing in jump height or airSpeed until he releases the key,but I'm not sure how to do that. – Bart Apr 16 '23 at 17:31
  • I like @OldDogProgrammer's suggestion. I'd use key bindings. Get the time when the space key is pressed, and get the time when the space key is released. Calculate the height based on elapsed time. – Gilbert Le Blanc Apr 16 '23 at 19:55
  • @GilbertLeBlanc yes, I have the height calculated but the problem is how I update the player's Y position or airSpeed based on this, I don't know how to do that – Bart Apr 16 '23 at 20:02
  • What's the maximum jump height in pixels? What's the minimum jump height in pixels? How many pixels per second does the player move when jumping? What's the frame rate? – Gilbert Le Blanc Apr 16 '23 at 21:34
  • How long does the player hold down the space key to jump to maximum height? – Gilbert Le Blanc Apr 16 '23 at 21:41
  • I define most of these variables in the code above. The game renders the drawing 60 times per second and updates the game 200 times per seconds. I can set different jump heights by modifying jumpHeight to 2.0 * SCALE for a small jump or 8.0 * SCALE for a big jump, I want the player to be able to jump between these values depending on how long he presses jump, or just a few if statements depending on how long he's holding, e.g <1 sec: 2.0 jumpheight, 1-2 sec: 4.0 jumpheight, >2 sec: 6.0 jumpheight. The player moves with airSpeed upwards – Bart Apr 16 '23 at 23:36
  • You want the jump to begin when the key is pressed, and continue while the key is held or the maximum jump is reached? When I saw the question, I thought the jump would start when the key is released. This is more consistent with reality: When I jump, my path is pretty much set at the moment my feet leave the ground. – Old Dog Programmer Apr 17 '23 at 00:27

0 Answers0