[LibGdx] Animation not working

I can’t figure out why my code doesn’t work. Can anyone tell me why?
public void create() {
batch = new SpriteBatch();
atlas = new TextureAtlas(Gdx.files.internal(“output/scorpion.pack”));
walkTextureRegion = new TextureRegion[10];

//image inside my pack are named “0” , “1” ,“2” and so on
for (int i = 0; i < 10; i++) {
walkTextureRegion[i] = atlas.findRegion("" + i);
}
walkAnimation = new Animation(0.06f, walkTextureRegion);

}

@Override
public void render() {
    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    timePassed += Gdx.graphics.getDeltaTime();
    batch.begin();
    batch.draw(walkAnimation.getKeyFrame(timePassed, true), 50, 50);
    batch.end();
}

}

Saying “it doesn’t work” and then putting your code here doesn’t help us figure out your problem. What exactly doesn’t work? What error is it throwing?

It would also help to put your code in [ code][ /code] tags for readability.

I think with how TextureAtlas work is that images with a number appended to them (or underscore number) will be grouped under the original name and have a special index. Maybe try putting that number as the second parameter instead.
[icode]
for (int i = 0; i < 10; i++) {

  • walkTextureRegion = atlas.findRegion("", i);
    }
    [/icode]

whoops formatting got messed up

It should work like this:


  atlas = new TextureAtlas(Gdx.files.internal("output/scorpion.pack"));
  animation = new CustomAnimation(.06f, atlas.getRegions());

Although I’m not sure if they’re going to be in order, so chrislo27 might be correct.

I use CustomAnimation which is how I do it since I hate creating stateTime variable for each Animation and this is the code:


import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;

/**
 * @author Ivan Vinski
 * @since 1.0
 */
public class CustomAnimation extends Animation {
  private float stateTime;
  private boolean playing;

  public CustomAnimation(float frameDuration, Array<? extends TextureRegion> keyFrames) {
    super(frameDuration, keyFrames);
  }

  public CustomAnimation(float frameDuration, Array<? extends TextureRegion> keyFrames, PlayMode playMode) {
    super(frameDuration, keyFrames, playMode);
  }

  public CustomAnimation(float frameDuration, TextureRegion... keyFrames) {
    super(frameDuration, keyFrames);
  }

  public void play() {
    if (!playing) {
      playing = true;
    }
  }

  public void pause() {
    if (playing) {
      playing = false;
    }
  }

  public void stop() {
    if (stateTime != 0f) {
      stateTime = 0f;
    }
    if (playing) {
      playing = false;
    }
  }

  public void update(float delta) {
    if (playing) {
      stateTime += delta;
    }
  }

  public TextureRegion getKeyFrame(boolean looping) {
    return getKeyFrame(stateTime, looping);
  }

  public TextureRegion getKeyFrame() {
    return getKeyFrame(stateTime);
  }

  public int getKeyFrameIndex() {
    return getKeyFrameIndex(stateTime);
  }

  public boolean isAnimationFinished() {
    return isAnimationFinished(stateTime);
  }

  public boolean isPlaying() {
    return playing;
  }
}