Sprite Animation Library?

I’m currently looking for a good way to animate SpriteSheets. I understand the basics of how to go from frame to frame over a given duration and everything. However I need some advanced capability that I just can’t figure out myself.

I need to be able to start new animations when an animation is over, etc. For example, have an animation where the player starts walking, one looping walk animation, and one where the player stops walking, then goes back to the idle one.

Is there some library that would be able to do this, or can someone point in the right direction? I’m completely lost on where to go with this.

You could have each row in your spritesheet be a different animation, then when the animation needs to change, just switch rows on the sheet.

It’s a simple finite state machine with the states as row indices. (of course there’s also an inner FSM for the actual animation, that maintains which column on the sheet you’re at, but you probably already knew that)

You really do not need to use a library.

If you setup a StateMachine for your entity, it will be fairly simple. I’ll post an example using a simple finiteState system, rather than a full blown StateMachine:



public enum State{
WALKING,
RUNNING,
IDLE;


}

public class Enemy{

State state = Idle;

Animation currentAnimation;

// maybe like this?
HashMap<String, Animation> animations = new Hashmap<String, Animation>();

public Enemy(){

animations.put("walking", new WakingAnimation());
animations.put("running", new RunningAnimation());
animations.put("idle", new IdleAnimation());

}

public void update(){

currentAnimation.draw();

}

public void changeState(State state){
this.state = state;

switch(state){
case:
WALKING:
currentAnimation = animations.getKey("walking");
break;
}
// So on and so forth
}

}

Thanks, I think between I have it mostly figured out.

Another quick question, relating more to timing. If say the player starts walking in a direction, so I change the player’s state to START_WALKING, then I want to change his state to WALKING when the animation for START_WALKING has finished, do you have any tips on doing that? I’m not too experience when it comes to timing related stuff.

Create an actual StateMachine I guess.

That is a tiny bit more complicated, you basically have the StateMachine update constantly in your character update method, once that state has finished just change the state over to a new one.

If that makes sense, I don’t like to promote shit but I have a StateMachine and State class setup if you want to pinch it.

Since a state machine is basically “if the current situation is X and Y happens, switch to state Q and do Z,” then it would be simple:


<pseudocode>

if(state == State.START_WALKING) {
    /* stuff to increment animation, display, etc */
    
    if(frame == Animations.START_WALKNIG.frameCount - 1) { //we're on the last frame
        state = State.WALKING;
        frame = 0; //reset to beginning
    }
}
...