1
votes

I want to create game like a flappy bird. I want player to jump continuously on the screen. I created this code, and it's not like a flappy bird jumping

Code:

float jump = 100; // Just example

if(Gdx.input.justTouched())
    body.applyForceToCenter(0, jump * delta, true);

The problem is when user fast tap on screen, the player is shot like a rocket. Also when player falling the jump is lower. How can I fix this, and get always the same jump strength?

My solution:

    jumpTimer += delta;

    if(Gdx.input.justTouched()) {

        if (jumpTimer > jumpTime) {

            body.setLinearVelocity(body.getLinearVelocity().x, 0);
            body.applyForceToCenter(0, jump * delta, true);
        }

        jumpTimer = 0;
    }
1
Clamp delta between 2 appropriate values so it's never too high or too low. Upon a jump, set the velocity of the body on the y-axis to 0 before applying the force. - user123

1 Answers

0
votes

Several methods can be used to achieve such an effect.

  • Like Mohammad mentioned in a comment you can clamp the y-velocity to a max value.
  • Make it so the player can only tap the screen once every x time.
  • The faster the player is going up, lesser up force is generated by tapping.
  • Completely disallow tapping at a certain velocity.
  • Combine these and tweak it till your happy with the results.