Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm developing a platformer game and have implemented jumping for the player. The player is able to jump, but holding the up arrow causes the player to continuously jump. What would be the best way to add a small delay between jumps so that once the player lands on the ground; there is a small delay before the next jump is initiated.

share|improve this question

5 Answers 5

up vote 5 down vote accepted

Adding an artificial delay is a really bad idea IMHO.

1) It's unexpected - what other well-known platformer does it that way?

2) It limits your player's jumping accuracy, because in tight situations, it might prove fatal (the character falls off a platform because you can't take off quickly enough), which would be the ultimate turn-off for any player - losing against the interface

3) To compensate for 2), you would have to tone down the maximum difficulty of your jumping areas. That limits how challenging and interesting you can make your game.

4) It still doesn't actually solve the problem of spamming - if you just keep holding jump, then the character will jump after the delay, like a robot. Is that really any better than jumping like a robot without delay?

There is, however, an organic solution to the problem: You just have to remember that human coordination is limited by nature. The only way for me to spam jumping is by holding the jump button. If I can't just spam the jump key, then I have to wait until my character has landed before pressing jump again. That poses a natural limit on how fast I can jump, and an artificial delay becomes completely unnecessary.

All you have to do is to check for 3 things before jumping:

  • Is the state of the jump key currently "pressed"?
  • Is the character on the ground?
  • In the previous frame, was the state of the jump key "not pressed"? (you have to save the previous key state of course)

If all checks are true, then the character jumps.

The great upside to this method is that I can actually improve my jump frequency by training, and really, that's what the vast majority of players want from their games: Getting better at it. With an artificial limit, you set a hard limit on how good I can become at jumping in your game, and if I feel I could do better but you don't let me, I will be disappointed and stop playing quickly.

share|improve this answer
    
They have to press the jump key exactly one frame after releasing it to jump? Your three checks seem to not make sense, or I'm not reading them right. –  Byte56 May 24 '12 at 22:34
    
@Byte56 I think you've misread, or maybe I used the wrong grammar, I'm not a native speaker. In code, I meant if(JumpKeyPressed && IsCharacterOnGround() && !prevJumpKeyPressed), i.e. the jump key is being pressed now, but it wasn't the frame before. Basically what jhocking said, but explained in more detail. –  Hackworth May 24 '12 at 22:38
    
Yes that's clearer now. Your intention is to mean that they just pressed the key. –  Byte56 May 24 '12 at 22:48
    
Thanks for the advice, I have taken this approach instead –  Sun May 26 '12 at 17:45

Or you can simply use instead of input.isKeyDown(Input.YOURKEYHERE) use input.isKeyPressed(Input.YOURKEYHERE). With that you will go to the if statement only once per button and the player will jump only once.

share|improve this answer
    
+1 This may not necessarily solve the problem question's author had in mind, but it's a simple, neat solution to actually react only once per one key stroke. –  Markus von Broady Oct 11 '12 at 8:40

You can add a little timer:

public void jump() {
   if(gameTime.getTimeMS() - jumpTime > jumpDelayInMilliseconds) {
       jumpTime = gameTime.getTimeMS();
       character.jump();
   }

Basically you are noting the last time your character jumped, then ensuring that enough time has passed before issuing the jump command again.

Note that the code above is not specific to Slick2D, just javaish pseudo code.

share|improve this answer
    
One could also start this timer when the player hits the ground, which would prevent them from 'spamming' jumps, which makes animations look funny in some games. –  SomeGuy May 24 '12 at 21:30
    
Perfect, this works great. –  Sun May 24 '12 at 21:46

I don't think you want a time delay between jumps; what you want is to only jump once when a button is pressed. That is, don't jump over and over while the button is held down, require a new button press each time.

share|improve this answer

You can use this little trick :

if(Jump == true){

        if(JumpFall == false){
            shiftY -= 1;
        }
        if(shiftY == PUT_YOUR_JUMP_HEIGHT_HERE){
            JumpFall = true;
        }
        if(JumpFall == true){
            shiftY += 1;
        }
        if(shiftY >=PUT_YOUR_STARTING_HEIGHT_HERE){
            Jump = false;
            JumpFall = false;
        }
    }  

So , Jump is a boolean variable telling that now you are jumping , shiftY is an int variable for player's height and JumpFall is a boolean variable telling that you have to fall now ,the jump is over .So, adding this part of code into your game loop (update loop in Slick2D) and setting Jump to True when you press the Up Key for example will make your character jump from your starting height (for instance, the ground) and jump up to your jump height (for example till reaching a bonus box) and then you will see your player going down to the ground like super mario does. The Last if() just Initializes the whole group.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.