2D Toolkit Forum

2D Toolkit => Support => Topic started by: dotty on February 26, 2014, 10:33:42 pm

Title: How do I go about managing multiple animations on different keypresses?
Post by: dotty on February 26, 2014, 10:33:42 pm
Hello, I'm having problems manning multiple animations. Here's some pseudo code.

if( move left button ){
  play move left animation
}

if( move right button ){
  play move right animation
}

if( punch button ){
  play punch animation
}

if( kick button ){
  play kick animation
}

if( no buttons pressed ){
  play idle animation
}

Generally if no buttons are pressed I want to play a looping idle animation. However, if move right and move left buttons are pressed I want to play a looping moving animation. However, if I press the punch button I want to play the punch animation.

However, the punch animation isn't interruptible. So I can only punch once. Also if I hold the move right button, the move right animation should play, but if I hit punch that should happen, then revert to the right move animation.

I've tried to program all of this, but there's a lot of ifs and else that it's getting extremely messy for something that should be quite simple.

How would you do this? Are there any common patterns to follow?
Title: Re: How do I go about managing multiple animations on different keypresses?
Post by: unikronsoftware on February 27, 2014, 01:39:41 pm
You should probably split up the animations from the state.
Eg. if (move_left) move = -1; and so on.

Later in the function, set the appropriate clip playing.
if (punch_button_pressed) {
    anim.Play("Punch");
}

if (!anim.IsPlaying("Punch")) {
    switch (move) {
         case -1: anim.Play("Left"); break;
         ...
   }
}

This shouldn't be much more complicated than the above.
Title: Re: How do I go about managing multiple animations on different keypresses?
Post by: dotty on February 27, 2014, 01:51:32 pm
How about if kicks, jump kicks (jump, then kicks) are added to the mix?
Title: Re: How do I go about managing multiple animations on different keypresses?
Post by: unikronsoftware on February 27, 2014, 01:55:17 pm
One shots should be handled the same as punch. Just have a list of the ones you care about, then anim.IsPlaying and anim.CurrentClip.wrapMode == Once. Basically your logic is, if you're not playing a one shot play the motion related one.

Once you have your vertical and horizontal movement (eg. jumping = vertical velocity is > 0) pick the appropriate clip to play based on the state.