Reputation: 21
My code:
if(key == KeyEvent.VK_SPACE){
tempObject.setJumping(true);
tempObject.setVelY(-10);
}
In this code, when I press the SPACE key, the player jumps, clearly. Except that is not where it ends, or else I would not be doing this. When I hold the SPACE bar, the player flies. I tried a timer, but it will not allow rapid jumping, which will be needed for a platformer. Thanks in advance.
Upvotes: 0
Views: 87
Reputation: 1609
Try creating a JumpingState
enum:
enum JumpingState {NOT_JUMPING, JUMPING}
if (key == KeyEvent.VK_SPACE && jumpingState == JumpingState.NOT_JUMPING) {
jumpingState = JumpingState.JUMPING;
tempObject.setJumping(true);
// Start timer here to revert tempObject and set jumpingState back to NOT_JUMPING
}
in conjunction with a timer that can be used to control when the player is currently jumping. Any jump event that comes in while the player is currently jumping can simply be ignored based on checking JumpingState
.
Upvotes: 2