Hello Guest

Author Topic: How do I go about managing multiple animations on different keypresses?  (Read 3438 times)

dotty

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 65
    • View Profile
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?
« Last Edit: February 27, 2014, 09:30:40 am by dotty »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How do I go about managing multiple animations on different keypresses?
« Reply #1 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.

dotty

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 65
    • View Profile
Re: How do I go about managing multiple animations on different keypresses?
« Reply #2 on: February 27, 2014, 01:51:32 pm »
How about if kicks, jump kicks (jump, then kicks) are added to the mix?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: How do I go about managing multiple animations on different keypresses?
« Reply #3 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.