0
\$\begingroup\$

I've made an animation class that creates an animation for a player. However, I'm wondering how I can change the animation depending on where the player is currently moving? Heres my animation class:

public class AnimationTest extends ApplicationAdapter{
Batch batch;
private TextureAtlas textureAtlas;
private Animation animation;
private float elapsedTime = 0f;
private Sapien player;

public void create (Batch batch, String string, Sapien player) {
    this.batch = batch;
    this.player = player;
    textureAtlas = new TextureAtlas(Gdx.files.internal(string));
    animation = new Animation(1f/10f, textureAtlas.getRegions());
}

public TextureAtlas getTextureAtlas(){
    return new TextureAtlas(Gdx.files.internal("Player/Angle1/running/running.atlas"));
}

@Override
public void dispose() {
    batch.dispose();
    textureAtlas.dispose();
}

@Override
public void render () {
    elapsedTime += Gdx.graphics.getDeltaTime();
    //Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw((TextureRegion) animation.getKeyFrame(elapsedTime,true),player.getX(),player.getY(),75,75);
    batch.end();
}

I currently call create() and render() in another class.

public TiledTestTwo (int scale){
    this.scale = scale;
}

@Override
public void create () {
    View.createInstance(this.scale);
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();

    player = new Player(animationTest.getTextureAtlas(),400,400,2);
    playerController = new PlayerController(player);
    camera = View.getInstance().createCamera(w, h);

    animationTest.create(View.getInstance().getBatch(), "Player/Angle1/idle/angle1IdleSheet.atlas", player);

    rounds = new Rounds(scale,w, h);
}

@Override
public void render () {
    //Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    //animationTest.render();
    camera.update();
    View.getInstance().setView(camera);

    View.getInstance().render();
    animationTest.render();
    movableSubject.notifyUpdate();
    collisionController.checkCollisions(View.getInstance(), player, this.scale);

    ZombieObserver.getInstance().playerLocation((int) player.getX(),(int) player.getY());
    rounds.checkNewRound(player);
}
\$\endgroup\$
1
  • \$\begingroup\$ Please, include additional information about how your code manages movement rather than just providing the source code. This way we'll be able to read it more quickly instead of first trying to understand what it does. \$\endgroup\$
    – liggiorgio
    Commented Oct 5, 2021 at 8:57

1 Answer 1

0
\$\begingroup\$

This is often achieved by loading all the relevant Animations in a separate class, and then referring to those animations from your player class based on some condition.

For example:

public class PlayerAnimations {
  public Animation<TextureRegion> walkLeft;
  public Animation<TextureRegion> walkRight;

  public PlayerAnimations() {
    walkLeft = new Animation(1f/10f, new TextureAtlas(Gdx.files.internal("player/walk/left.atlas")));      
    walkRight = new Animation(1f/10f, new TextureAtlas(Gdx.files.internal("player/walk/right.atlas")));      
  }
}

Then, in your player's render method you can check which direction the player is moving and pick the correct animation for that direction:

@Override
public void render () {
  elapsedTime += Gdx.graphics.getDeltaTime();
  Animation<TextureRegion> animation; // This is no longer set in the constructor, but picked here in render depending on the direction.

  if (isMovingLeft()) 
    animation = playerAnimations.walkLeft;
  else
    animation = playerAnimations.walkRight;
  batch.begin();
  batch.draw((TextureRegion) animation.getKeyFrame(elapsedTime,true),player.getX(),player.getY(),75,75);
  batch.end();
}
\$\endgroup\$
1
  • \$\begingroup\$ Thanks a lot. I actually found a way to do it before you posted your suggestion, in a similar manner! The thing I didn't understand was that I could call (in your case PlayerAnimation) in render.+ \$\endgroup\$
    – Knightyy
    Commented Oct 5, 2021 at 9:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .