I think the easiest way to handle this would be to activate the jump code on the Button Released function.
When the button is down, start a counter (placing a max on how big the jump can be), when the button is released, activate the jump.
1 2 3 4 5 6 7
| int jumpPower = 0 while (buttonDown){ if(jumpPower < maxJumpPower){ jumpPower++; } } doJump(jumpPower); |
Hope this helps,
-Pickle
This would give a "charge-up" kind of effect. He wants the jump to happen immediately, but result in a higher jump the longer the button is pressed.
I like the idea someone said previously of setting an "isJumping" flag. And really it should be a value. On update, check to see if the jump button is down. If it is, increment isJumping.
Then check to see if isJumping has reached the maximum number of frames that you allow the jump key to be held for.
Once they either let go of the jump key or the max is reached, set isJumping to 0.
Then, all you have to do in your calculations is only apply gravity (negative acceleration that alters velocity) if isJumping is 0.
adding some pseudocode..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| function update() { if(Key.isDown(SPACEBAR) && true == canJump) { if(isJumping == 0) { yV = -100; } isJumping++; } if(isJumping > maxJumpTime) { isJumping = 0; } if(isJumping == 0) { canJump = false; yV += gravity; } this.y += yV; } |