[LibGDX] Animation + Delta Time?

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;
	}
}

getDeltaTime returns a float (the number of seconds between frames, a very small number generally). Ints truncate it back to zero. Change frameDelta to a float.

EDIT: ints don’t always truncate a float/double to zero. They will return it to the whole number. They do not round (2.9 float becomes 2 truncated to int).

Also, just pointing out that when you say new Animation(2f, …) your animation will change frames every two seconds, which is pretty slow (unless that’s intentional). That number where your 2f is now is the time for each frame of the animation.

Ok, thanks for that help but also how would I have it so when I press left and release left the player will change its idle stance to left instead of reverting back to right?

I didn’t pick this up earlier but you CANNOT compare strings with ==. They are immutable objects, and must be compared with .equals(). You’ll also want to rid the “else” from the first if statement to check the idle stances. If you’re using Eclipse you can press Control+Shift+F to reformat all the code to look better and become more readable (this also annoyingly formats comments – you can change this in the preferences if you’d like). Using a string for left and right stances is pretty slow when you’re comparing the strings to determine what idle stance you want. You should use an Enum, integer, or even a boolean.

Modified update method:

public void update(){
      if(Gdx.input.isKeyPressed(Keys.A)){
         CurrentAnimation = WalkLeftAnimation;
         directionString = "Left";
         
      }
      if(Gdx.input.isKeyPressed(Keys.D)){
          CurrentAnimation = WalkRightAnimation;
          directionString = "Right";
      }

       if(directionString == "Left"){
          CurrentAnimation = IdleLeftAnimation;
       }
       else if(directionString == "Right"){
          CurrentAnimation = IdleRightAnimation;
       }
      
   }

For clarity, immutability isn’t why you can’t use == on Strings for equality. It’s because string (and most datatypes) equivalence is not object identity (pointer equivalence).

Just be glad this isn’t completely insane like in JS: https://dorey.github.io/JavaScript-Equality-Table/unified/ http://stackoverflow.com/a/359509

Oops, I was thinking of something else. I have no idea where I got immutability (I was reading something on primitives earlier, maybe that bled in). Thanks for correcting me!