1

I made a jumping animation but when i stop touching the screen, the animation stops.

Here is the code:

    boolean touching = Gdx.input.isTouched();
    long elapsed = (System.currentTimeMillis() - startTime)%1000;

    if (touching){
        float jump = 500 * (float)Math.sin(((Math.PI*2)/1000)*(elapsed/2));
        sprite.setPosition(Gdx.graphics.getWidth()/2,jump);
    }

How do i make the animation finish after i stop touching the screen?

Here is a gif of the code running:

enter image description here

  • your current result `stop touching the screen, the animation stops.` and you want to achieve `animation finish after i stop touching the screen`, what is difference between these two, I think only of word `stop` and `finish` ? – Abhishek Aryan May 22 '17 at 10:16
  • When i stop touching, the sprite freezes in the air. But i want it to finish the animation until it hits the ground –  May 22 '17 at 15:19
  • okay got it, already answered check that – Abhishek Aryan May 22 '17 at 15:23

1 Answers1

1

I think what you want to achieve is to jump by a single touch and let it finish. I would use somekind of state to do this. Currently, the code within your statement only runs when touching.

Whenever a user touches the screen you could set a boolean jumping to true. Then instead of checking if (touching) you check for if (jumping). Here you put the code for the jump. When someone presses the screen while jumping you should not allow it to reset (except in the case of something like a double jump). And whenever the entity touches the ground again jumping should be set back to false.

Madmenyo
  • 8,389
  • 7
  • 52
  • 99