I am making a 'simple' top-down space shooter in 2d using Libgdx. I have loaded the texture regions into arrays and created Animations inside my sprite, but when I call the getKeyFrame it seems to jump straight to the last frame in the spritesheet.
The spritesheet is 9 frames in total of a spaceship banking to the left (hence I have enabled FlipX for when turning the other way. But all works as I expected apart from that it just jumps to the last frame in the sequence rather than the gradual banking animation.
The class in question is below, please do let me know if you need to view the rest of the code:
package com.moneylife.spaceattackers.Sprites;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
import com.moneylife.spaceattackers.Screens.PlayScreen;
public class Player extends Sprite {
private PlayScreen playScreen;
private Animation bankRight, bankLeft;
private int totalFrames = 9;
private int frameWidth = 100, frameHeight = 100;
private float frameDuration = 0.3f;
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(frameDuration, frames);
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(frameDuration, frames);
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){
switch (currentFlyState){
case STRAIGHT: {
setRegion(straightFlyingTextureRegion);
setSize(frameWidth, frameHeight);
break;
}
case LEFT: {
setRegion(bankLeft.getKeyFrame(frameDuration, false));
break;
}
case RIGHT: {
setRegion(bankRight.getKeyFrame(frameDuration, false));
break;
}
}
if (Gdx.input.isKeyPressed(Input.Keys.A)){
currentFlyState = flyState.LEFT;
}
else if (Gdx.input.isKeyPressed(Input.Keys.D)){
currentFlyState = flyState.RIGHT;
}
else
currentFlyState = flyState.STRAIGHT;
}
}
I beleive that the issue must be in the frames.add part (is it perhaps overwriting each frame with the other until the end??) but I cant figure out how to fix this. Can somebody please see what I am missing here?
EDIT: I have also tried moving " bankLeft = new Animation(frameDuration, frames); frames.clear();" moving that from the For loops to just below the for loop. But that meant I only got the 2nd frame in the animation only for some reason :S So my constructor now looks like this, but neither work as needed :(
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(frameDuration, frames);
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(frameDuration, frames);
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);
setSize(frameWidth, frameHeight);
}