(LibGdx) Controlling an animation

I’m trying to get an animation to start until it finished whenever I click the screen. I can’t get it to stop looping.

public class DungeonedFate implements ApplicationListener {
	
	boolean animateIt = false;
	
	
    private static final int        FRAME_COLS = 6;         // #1
    private static final int        FRAME_ROWS = 5;         // #2

    Animation                       walkAnimation;          // #3
    Texture                         walkSheet;              // #4
    TextureRegion[]                 walkFrames;             // #5
    SpriteBatch                     spriteBatch;            // #6
    TextureRegion                   currentFrame;           // #7

    float stateTime;                                        // #8

    @Override
    public void create() {
        walkSheet = new Texture(Gdx.files.internal("sprite-animation1.png")); // #9
        TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS);              // #10
        walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
        int index = 0;
        for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                walkFrames[index++] = tmp[i][j];
            }
        }
        walkAnimation = new Animation(0.025f, walkFrames);      // #11
        spriteBatch = new SpriteBatch();                // #12
        stateTime = 0f;                         // #13
    }

    @Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);                        // #14
        stateTime += Gdx.graphics.getDeltaTime();           // #15
        currentFrame = walkAnimation.getKeyFrame(stateTime, animateIt);  // #16
        spriteBatch.begin();
        spriteBatch.draw(currentFrame, 50, 50);             // #17
        spriteBatch.end();
        
        checkClick();                               ////////////////////
    }
    
    public void checkClick() {
    	if(Gdx.input.isButtonPressed(0)) {
    		animateIt = true;
    		
    		System.out.println("Start you animation!");
    	}
    }

as you can see it loops once, then when my (invisible) mouse clicks the screen; it restarts and never stops. This is as close as I could get after failing miserably multiple times.

If I’m not mistaken the second parameter of getKeyFrame is the looping parameter. Once you click animateIt it is set to true, hence your animation starts to looping.

Change following things:

  • Only update state time when animateIt is true.
  • Use false as second parameter in getKeyFrame
  • Reset the state time to 0 when the mouse has been clicked.

With this the image will be the first key frame of the animation, until you click. When you click the animation plays and should stop at the last key frame. When the user clicks again, it starts from beginning.


@Override
public void render() {
	Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);                        // #14
+	if(animateIt){
		stateTime += Gdx.graphics.getDeltaTime();           // #15
+	}
-	currentFrame = walkAnimation.getKeyFrame(stateTime, animateIt);  // #16
+	currentFrame = walkAnimation.getKeyFrame(stateTime, false);  // #16
	spriteBatch.begin();
	spriteBatch.draw(currentFrame, 50, 50);             // #17
	spriteBatch.end();
	        
	checkClick();                               ////////////////////
}

public void checkClick() {
	if(Gdx.input.isButtonPressed(0)) {
		animateIt = true;
+		stateTime = 0f;                           		
		System.out.println("Start you animation!");
	}
}
    

It would probably be good to check if the animation has been completed and set animateIt to false, if so.

Thanks, it worked perfectly. I can’t medal you for some reason sadly.

I appreciated on your behalf :slight_smile:

A thanks is fine. Glad it worked.