Skip to main content
added 17 characters in body
Source Link
Jacob
  • 2.6k
  • 3
  • 19
  • 50

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;

And you're ready to go!

Now I read it all. You can keep your frameDutation (make it final) - and pass it to the constructor, but make another variable like stateTime 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);
        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);
        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;

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 frameDuration (make it final) - and pass it to the Animation's constructors, but make another variable like stateTime 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;
Source Link
Jacob
  • 2.6k
  • 3
  • 19
  • 50

public TextureRegion getKeyFrame (float stateTime, boolean looping)

It doesn't take a 'frame time' - it takes the time now (and you should add delta time to it every frame).

Instead of calliing

setRegion(bankLeft.getKeyFrame(frameDuration, false));

change the frameDuration name for stateTime and make it 0f in constructor:

float stateTime = 0f;

in update call:

stateTime += dt;
setRegion(bankLeft.getKeyFrame(stateTime, false)); // for both cases

And you're ready to go!

Edit

Now I read it all. You can keep your frameDutation (make it final) - and pass it to the constructor, but make another variable like stateTime 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);
        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);
        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;