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.