I run an animation when Gdx.input.justTouched()
, but when the button is clicked, the animation is seen for a thousandth of a second. It's too speed, but I'm not talking about new Animation(0.25f)
.
Example of my code:
if (Gdx.input.justTouched()) {
boomAnimation.elapsedTime = Gdx.graphics.getDeltaTime();
game.batch.draw((TextureRegion) boomAnimation.animation.getKeyFrame(boomAnimation.elapsedTime, true), 137, 121, 80, 80);
}
CodePudding user response:
Gdx.input.justTouched()
returns true only once, when the screen is touched, but not when the contact remains. Therefore the animation is just shown for a single frame.
Try using Gdx.input.isTouched()
instead. This will return true, as long as the screen is touched.
Also see this answer.
EDIT
If you don't want the button to be held down to start the animation you can use a boolean flag to indicate whether the animation should be playing:
private boolean playAnimation = false;
// ...
public void render() {
if(Gdx.input.justTouched()){
playAnimation = true;
}
if (playAnimation) {
boomAnimation.elapsedTime =Gdx.graphics.getDeltaTime();
game.batch.draw((TextureRegion) boomAnimation.animation.getKeyFrame(boomAnimation.elapsedTime,true), 137,121,80,80);
}
}