Alright, so I have created multiple sprite-sheets for the my players different animations and put them in the game. Problem is, there is no animation, just the sprite. Also, when I press a button and release it ‘DirectionString’ sets itself back left no matter what. I’m using an int called ‘frameDelta’ which has delta Time added to it to keep track of what frame to draw (Although I have failed at that) and am stuck on whats wrong. I successfully implemented an Animation in another section of my code this exact same way and had no issues.
public class Player {
private Vector2 position, velocity;
private TextureAtlas WalkLeftAtlas, WalkRightAtlas, IdleLeftAtlas, IdleRightAtlas;
private Animation CurrentAnimation,WalkLeftAnimation, WalkRightAnimation, IdleLeftAnimation, IdleRightAnimation;
private int frameDelta = 0;
private String directionString;
private SpriteBatch batch;
public Player(int x, int y, Camera camera){
position = new Vector2();
this.position.x = x;
this.position.y = y;
WalkLeftAtlas = new TextureAtlas(Gdx.files.internal("data/Graphics/Entities/PlayerWalkLeft.atlas"));
WalkRightAtlas = new TextureAtlas(Gdx.files.internal("data/Graphics/Entities/PlayerWalkRight.atlas"));
IdleLeftAtlas = new TextureAtlas(Gdx.files.internal("data/Graphics/Entities/PlayerIdleLeft.atlas"));
IdleRightAtlas = new TextureAtlas(Gdx.files.internal("data/Graphics/Entities/PlayerIdleLeft.atlas"));
WalkLeftAnimation = new Animation(2f, WalkLeftAtlas.getRegions());
WalkLeftAnimation.setPlayMode(Animation.LOOP);
WalkRightAnimation = new Animation(2f, WalkRightAtlas.getRegions());
WalkRightAnimation.setPlayMode(Animation.LOOP);
IdleLeftAnimation = new Animation(2f, IdleLeftAtlas.getRegions());
IdleLeftAnimation.setPlayMode(Animation.LOOP);
IdleRightAnimation = new Animation(2f, IdleRightAtlas.getRegions());
IdleRightAnimation.setPlayMode(Animation.LOOP);
CurrentAnimation = IdleLeftAnimation;
batch = new SpriteBatch();
}
public void render(){
frameDelta += Gdx.graphics.getDeltaTime();
batch.begin();
batch.draw(CurrentAnimation.getKeyFrame(frameDelta, true), position.x, position.y);
batch.end();
}
public void update(){
if(Gdx.input.isKeyPressed(Keys.A)){
CurrentAnimation = WalkLeftAnimation;
directionString = "Left";
}
if(Gdx.input.isKeyPressed(Keys.D)){
CurrentAnimation = WalkRightAnimation;
directionString = "Right";
}
else if(directionString == "Left"){
CurrentAnimation = IdleLeftAnimation;
}
else if(directionString == "Right"){
CurrentAnimation = IdleRightAnimation;
}
}
public Vector2 getPosition(){
return position;
}
}