And you're ready to go!
Also, move your new Animation, frames.add() and frames.clear() outside of loops.
Now I read it all. You can keep your frameDutationframeDuration (make it final) - and pass it to the constructorAnimation's constructors, but make another variable like stateTimestateTime as I said. Here's your rewritten code: (I wrote //fix where I did a fix!)
public class Player extends Sprite {
private PlayScreen playScreen;
private Animation bankRight, bankLeft;
private int totalFrames = 9;
private int frameWidth = 100, frameHeight = 100;
private final float FRAME_DURATION = 0.3f; // each frame will last 0.3f
private final float stateTime = 0f; // fix
private TextureRegion straightFlyingTextureRegion;
enum flyState { STRAIGHT, LEFT, RIGHT }
private flyState currentFlyState;
public Player(PlayScreen playScreen){
super(new Texture("ship.png"));
currentFlyState = flyState.STRAIGHT;
this.playScreen = playScreen;
Array<TextureRegion> frames = new Array<TextureRegion>();
for (int i = 0; i < totalFrames; i++){
frames.add(new TextureRegion(getTexture(), i * frameWidth, 0, frameWidth, frameHeight));
}
bankLeft = new Animation(FRAME_DURATION, frames);
// fix
frames.clear();
}
for (int i = 0; i < totalFrames; i++){
TextureRegion region = new TextureRegion(getTexture(), i * frameWidth, 0, frameWidth, frameHeight);
region.flip(true, false);
frames.add(region);
}
bankRight = new Animation(FRAME_DURATION, frames);
// fix
frames.clear();
}
straightFlyingTextureRegion = new TextureRegion(getTexture(),0,0,frameWidth, frameHeight);
setPosition(playScreen.viewport.getWorldWidth() / 2 - frameWidth / 2,0);
setRegion(straightFlyingTextureRegion);
bankLeft.setPlayMode(Animation.PlayMode.NORMAL);
bankRight.setPlayMode(Animation.PlayMode.NORMAL);
}
public void update(float dt){
stateTime += dt; // fix
switch (currentFlyState){
case STRAIGHT: {
setRegion(straightFlyingTextureRegion);
setSize(frameWidth, frameHeight);
break;
}
case LEFT: {
setRegion(bankLeft.getKeyFrame(stateTime, false)); // fix
break;
}
case RIGHT: {
setRegion(bankRight.getKeyFrame(stateTime, false)); // fix
break;
}
}
if (Gdx.input.isKeyPressed(Input.Keys.A)){
currentFlyState = flyState.LEFT;