0

Hi I have a spritebatch animation in Libgdx. It only plays once and when it is finished I want to stop drawing it so it doesn't appear on the screen. I have a boolean that checks if it is still alive i.e. animating and it is set to false when the animation is finished using isAnimationFinished however it stays on the screen and I am not sure why even though a log print shows it changing to false on finish. Does anyone have any idea why this is? Thanks for your help. The following code is from my render method.

Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
     boolean alive = true;
     stateTime += Gdx.graphics.getDeltaTime();           // #15
        currentFrame = walkAnimation.getKeyFrame(stateTime, false);  // #16
        spriteBatch.begin();
        spriteBatch.draw(menu, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        if (alive)
        spriteBatch.draw(currentFrame,50,50);
        if(walkAnimation.isAnimationFinished(stateTime))
        alive = false;
        System.out.println(alive);
        spriteBatch.end();
        stage.draw();
user2002077
  • 595
  • 1
  • 6
  • 17

1 Answers1

2

The problem I see is that you always declare a new boolean variable "alive" and set the alive to true at the beginning:

boolean alive = true;

and then all your drawing is happening and after all that you set alive to false if your animation is finished:

if(walkAnimation.isAnimationFinished(stateTime))
    alive = false;

==> That is too late, because after this you don't do anything anymore with this local variable.


You could for example check if "alive" should be true or false right at the beginning and set its value accordingly, like for example:

 boolean alive = true;
 if(walkAnimation.isAnimationFinished(stateTime)) {
    alive = false;
 }
 //your drawing code below..

alternatively you could make "alive" a field instead of a local variable.

donfuxx
  • 11,277
  • 6
  • 44
  • 76
  • 1
    Thank you very much for pointing that out. It was a stupid mistake I made. I made my boolean variable global instead and it works fine now. – user2002077 Apr 15 '14 at 15:58