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); } }
Recent Announcements
Continue the conversation
Join the Discord to ask follow-up questions and connect with the community
JC|HCL
Java Community | Help. Code. Learn.
We are a Community full of Java developers. Connect with other devs, get help, help others and do much more!