36865

How to stop drawing a SpriteBatch animation in libgdx?

Question:

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

Answer1:

The problem I see is that you <strong>always</strong> 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 <strong>after</strong> 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 <em>local</em> variable.

<hr />

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.

Recommend

  • Android detect Bitmap too large
  • problem loading texture with transparency with OpenGL ES and Android
  • Diffuse shader for OpenGL ES 2.0: Light changes with camera movement (Vuforia on Android)
  • OpenGL gimbal lock
  • libgdx TiledMap Rendering Performance Issue
  • Can't draw a ninepatch image and stage at the same time
  • LibGDX: Android SpriteBatch not drawing
  • Linker error with glew and Visual Studio on windows 7
  • responsive table with variable column number
  • How to initialize global variable at each class invocation?
  • how to replace TreeView's selected node with newly created node
  • How to wait for all async tasks to finish in Node.js?
  • Primefaces lazy datascroller calling load twice
  • Splash Screen will not display
  • How to access meteor package name inside package?
  • Rest Services conventions
  • Clear activity stack before launching another activity
  • SonarQube: Cannot deactivate rule with missing quality profile
  • Android full screen on only one activity?
  • Q promise. Difference between .when and .then
  • Read text file and split every line in MSBuild
  • C# - Serializing and deserializing static member
  • Java applet as stand-alone Windows application?
  • Controls, properties, events and timers running in design time
  • Updated Ionic CLI but shows previous version (Windows)
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • align graphs with different xlab
  • Return words with double consecutive letters
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • Error creating VM instance in Google Compute Engine
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal