How to stop drawing a SpriteBatch animation in libgdx?

风格不统一 提交于 2020-01-06 14:38:09

问题


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();

回答1:


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.



来源:https://stackoverflow.com/questions/23088315/how-to-stop-drawing-a-spritebatch-animation-in-libgdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!