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:
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
.