Hello Guest

Author Topic: Only some sprite animations not playing properly  (Read 4518 times)

cipher

  • Newbie
  • *
  • Posts: 13
    • View Profile
Only some sprite animations not playing properly
« on: June 10, 2013, 11:07:11 pm »
Hello,

I'm working on a platforming game as a disclaimer. I have an idle and walk animation play that play properly, but for some reason, my jump and attack animations only play the first frame and freeze there. Usually there's the workaround of telling it to only play if it's not already playing, but that doesn't seem to work. Am I missing something here? I'll post a few code snippets and an explanation.

inH is the Horizontal axis value, the grounded boolean is just if you are in the air or not

Code: [Select]
if(inH > 0) {
            if (!anim.IsPlaying("hero_WalkR")) {
                anim.Play("hero_WalkR");
            }
facingRight = true;
facingLeft = false;
isMoving = true;
        }
if(inH < 0) {
            if (!anim.IsPlaying("hero_WalkL")) {
                anim.Play("hero_WalkL");
            }
facingRight = false;
facingLeft = true;
isMoving = true;
        }

so here's a part of [walking] code, this all works fine. Scroll down a few lines to find the jump code, which only gets stuck on the first frame

Code: [Select]
if(grounded == false && inH > 0){
if (!anim.IsPlaying("hero_JumpR")) {
                anim.Play("hero_JumpR");
            }
facingRight = true;
facingLeft = false;
isMoving = true;
}
if(grounded == false && inH < 0){
if (!anim.IsPlaying("hero_JumpL")) {
                anim.Play("hero_JumpL");
            }
facingRight = false;
facingLeft = true;
isMoving = true;
}
          //later on in the code (for attacking, which also gets stuck on frame 1)
if(grounded == true && facingLeft == true){
if (!anim.IsPlaying("weapon1_groundL")) {
                anim.Play("weapon1_groundL");
anim.animationCompleteDelegate = null;
            }
}


I should also note I'm just one version of 2Dtoolkit behind the current one. I should also note that the grounded, facingLeft/Right, etc booleans all trigger properly for sure. I will happily provide anymore needed information.

Thanks

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Only some sprite animations not playing properly
« Reply #1 on: June 11, 2013, 03:59:07 pm »
Its really hard to tell why from the code. Can you add some debug spew to anim.Play( ... ) to print out the name of the clip each time its called? Is the output what you'd expect?

cipher

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Only some sprite animations not playing properly
« Reply #2 on: June 12, 2013, 06:21:51 am »
Yeah, the correct animations are triggering, and as expected, they are constantly triggering, thus not allowing the animations to progress. I spent some time trying to find out what differences there could be between the walk/idle code and the jump/attack code that could be causing the latter to not work, even though the setup was identical.

-The walk left and walk right animations are triggered solely by the horizontal getAxis value   (these work fine)

-The idle left and right animations are triggered by a facingLeft or facingRight bool, which is determined by the last animation that played, as well as the horizontal axis value being zero  (these work fine)

-The jump animations are determined by the horizontal axis, the previously mentioned facingLeft/facingRight bool, and a bool that triggers when you are on a platform ("grounded") (these do not work)

-The ground attack animations are triggered by the grounded variable, and the facingLeft/facingRight variable, and these only trigger if a custom getAxis value is not 0 (these do not work)

So by looking at this, the common thing between the two groups that do not work is that grounded variable, yet if I remove it from say, the ground attack animations (leaving it just the facingLeft/Right bool), it still does not work (same goes if I delete the other and just leave grounded).

The only other difference I can think of is that all the bools are ultimately called in the update function, as in they are constantly triggering, while the getAxis values are basically either 1, -1, or 0. I don't see how that would make a difference, and the idle animations use these same bools, and those DO work. I should also note that I have checked time and time again that all the booleans are triggering properly. So anyways, I'm pretty stumped here. I can't even exactly tell if it's something wrong with my code, or if it's a 2D toolkit issue...

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Only some sprite animations not playing properly
« Reply #3 on: June 12, 2013, 09:51:38 am »
Ok - this is a wild guess, but since you're running this in sequence, you could easily be overwriting the animations. Why not keep the animation bit COMPLETELY separate from the logic?

So the logic part does all the calculations and updates the state of the object, ultimately all you care about are:
facingDirection = Left | Right
state = Stationary | Moving | Jumping | Attacking

Once you've worked out the state, you need to decide what clip you should be playing. And finally, at one point in the code, change the currently playing clip if necessary.

cipher

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Only some sprite animations not playing properly
« Reply #4 on: June 13, 2013, 05:26:29 am »
Alas, I just tried rigging that, and was unsuccessful. Before I go on, i'll post a sample to make sure we had the same idea:

At the start of the function I have all the states and things seprated, this being a sample (once more, inH is the horizontal axis value):

Code: [Select]
if(inH > 0) {
facingRight = true;
facingLeft = false;
isMoving = true;
        }
if(inH < 0) {
facingRight = false;
facingLeft = true;
isMoving = true;
        }
if(inH == 0 && facingRight == true) {
isMoving = false;
        }

Once all of those are done (and they all work as intended), I have the animation sections (in the same function):

Code: [Select]
       if(isMoving == true && facingRight == true){
if (!anim.IsPlaying("hero_WalkR")) {
                                anim.Play("hero_WalkR");
                         }
}
if(isMoving == true && facingLeft == true){
if (!anim.IsPlaying("hero_WalkL")) {
                               anim.Play("hero_WalkL");
                        }
         }

...and so forth. Unfortunately the results are the same, with the idle and walk animations working fine, but the jump and attack ones getting stuck on frame one. I also tried making another new animation to put in for jump as a test, but that did not make a difference either. I'm going to go ahead and post this, and am going to try something else. Right now, everything is in a MovementAnimations function being called in update. I'll start playing around with putting some of these in difference places, see if that makes any difference.

Edit 1: No luck yet, but I swapped the actual movement animation with the attack animation, and it played properly. At the very least that rules out there being something wrong with the actual animations

Edit 2: It's already late, but I think I'm onto something here. I commented out the walking animation script, and then if I was moving and held down the attack button, the attack animation would play. I think it has something to do with the idle/walk animations perhaps overwriting the others? More investigation is required

Edit 3: Yup, that's the problem, I haven't quite figured it out yet though, but here's some other things I noticed. If I leave the walk animations commented out, and move the jump animations above the idle animations, then jump and the ground attack animations will play ONLY when you are moving. However by doing this, then the air attack animation won't even trigger at all, and of course there is no walk animation. If you have any suggestions that would always be helpful, but I think I'll need to keep rearranging things until I find something that works. I'll keep this thread updated.
« Last Edit: June 13, 2013, 06:25:34 am by cipher »

cipher

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Only some sprite animations not playing properly
« Reply #5 on: June 13, 2013, 06:55:10 am »
At last! Okay so I've figured it out, thought I'd post the solution here so you have it in your.. records in case this case ever comes up again.

So, in order to determine which animation to play, my code will check a series of states: facingLeft, facingRight, isMoving, isAttacking, and isGrounded. Basically what was happening was that, for example, the left movement animation needed the following circumstances to play: facingLeft, isGrounded, and isMoving. Now, the left attack animation needed all those variables, as well as the isAttacking boolean to be true. What was happening in this specific case is that the left movement animation didn't actually care if isAttacking is true or not, and thus would always overwrite the left ground attack animation.

In some shape or form, this is ultimately what was happening for all the animations that did not play properly. The reason it worked say, before I added the attack animations and so forth, was that there were at the time less variables to check. Thus, the solution is to create a unique circumstance for each animation that utilizes all states in their if statement, even if they are irrelevant. It seems really obvious now that I write it out, but it clearly didn't seem like that's what was causing the issue. I think what threw us off was the fact that the first frame of the animation that should be playing would play, now that I understand the issue, I wouldn't have thought it would have played at all.

Cheers