   2D Animation state transitions
  I'm implementing various animations in the game I'm working on and have already run into a maintenance nightmare with how I'm currently handling transitions between states of an object and the animations it plays.The current object I'm working on is the player, which has by far the most complex set of states and animation transitions.Right now I just keep a set of flags for various points of interest in the object:  self.flags = {    faceright = false,    grounded = false,    onladder = false,    lockjump = false,  -- allowed to jump?    shooting = false,    running = false,    jumping = false,    falling = false,    idle = false  }  self.animations = {    standing = { frames = {   { source = Rectangle(64, 0, 32, 32), length = 99.99999999 },   { source = Rectangle(0, 32, 32, 32), length = 1499.99999999 },   { source = Rectangle(32, 32, 32, 32), length = 149.99999999 } }, loop = true, keyframe = 2    },    walking = { frames = {   { source = Rectangle(64, 0, 32, 32), length = 116.66666666 },   { source = Rectangle(96, 0, 32, 32), length = 116.66666666 },   { source = Rectangle(128, 0, 32, 32), length = 116.66666666 },   { source = Rectangle(160, 0, 32, 32), length = 116.66666666 },   { source = Rectangle(128,  0, 32, 32), length = 116.66666666 } }, loop = true, keyframe = 2    }  }Each frame as the object is updated it will modify these flags and then it calls a "determine which animation I should play" function which uses the state of the flags to determine what to do.The problem with this is that I'm not actually detecting a transition between states, but rather the current state all the time.The reason that this is a problem is: some animations need to be played from the beginning while others do not. The idle animation, for example, needs to be played from the beginning because there are frames in the animation that are only supposed to be played once. On the opposite end of the spectrum: if you are running and press the shoot button then the "running and shooting" animation should be played but not restarted.Following this line of reasoning I end up with a function that looks like this for determining the animation:  if flags.idle then    self.animator:setAnimation(self.animations.standing)    self.animator:reset()  end  if flags.running then    self.animator:setAnimation(self.animations.walking)  endSo what happens is when the character is idle the idle animation is played and restarted each frame, resulting in the animation never actually playing.I need a new approach to this because what I have now isn't working. I'd like to build out a state-driven system but can't figure out how to design that at a high level.   
  states.If flag.WantsToRun && !state.IsRunning ){ StartAnim( Run ) state.IsRunnning = true;}//or (God this is an awful example forgive me)struct State{  virtual ~State( ) { }  virtual void Activate( ) { }};struct AnimState : public State{  int anim;  AnimState( int a ) : anim( a ) { }  virtual void Activate( ) { PlayAnim( a ); }};enum States { Idle, Run, Fall, Count };State *states[ Count ] = { new AnimState( Idle ), new AnimState( Run ), new AnimState( Fall ) };int currentState = Idle;void Update( ){  int newState = currentState;  if( flags.Run ) newState = Run;  else if( flags.Falling ) newState = Fall;  else newState = Idle;  if( newState != currentState )  {    currentState = newState;    states[ currentState ]->Activate( );  }};  There's two solutions i've seen to this problem...#1 - Break the upper and lower body in half.Break the body into 2 peices (upper and lower), each with their own animation state machine.  Let the lower half do the running and walking, let the upper half do the aiming and firing etc.You could have another machine for "full body" too that shows through if the upper body and/or lower body aren't active to help with doing full body animations.#2 - do "animation synchronization".Each animation state can have an "AnimationSyncGroup" identifier.  For our sakes lets consider them unique ID#'s.What happens is lets say you are playing an animation that has an AnimationSyncGroup of "5" and then you switch to another animation that has an AnimationSyncGroup of "5" too.  Instead of this new animation playing from the beginning, it will see how much time (or how much percent) the last animation was into it's animation, and it will start playing it's new animation from the same time.You'd have to set the animations up so that for run and walk cycle type animations that they had their feet in the same (or similar positions) across animations of the same animation sync group so that when you switch it doesn't look choppy.If you switch from an animation in one sync group to an animation in another sync group, you would just restart the animation from the beginning.In some ways, 2d animation can actually be A LOT more complicated than 3d animation.  In 3d you can do things like blend between animations to make smooth transitions, but in 2D you are stuck with the pixels you have, you can't blend 2 animations together in any real meaningful way :PEDIT: btw, i know a handful of 3d AAA games which use an animation selection system like you describe.There are other ways of doing animation selection but you are definitely on a path that should work for you (:   