Hello Guest

Author Topic: Calling Resume() on tk2dspriteanimator causing IndexOutOfRangeException?  (Read 3561 times)

bcullis

  • Newbie
  • *
  • Posts: 3
    • View Profile
Here's the setup:
In an animation clip, I've added a trigger on frame 1 with an int value indicating a further frame number.  This further frame is the "freeze" face of a critter in our game that's just been hit with a freeze attack.  In sequence, if they're hit with this attack, I
1) check for event trigger during play
2) if found, use its int value to set the new frame of animation
3) call Pause() to halt the animation in that frame.

Which works fine, it lets me define the exact pose in a "react to hit" animation that each critter type gets frozen in.  However, when they thaw back out, I want to just resume their animation.  When I call Resume(), I get the following:

IndexOutOfRangeException: Array index is out of range.
tk2dSpriteAnimator.ProcessEvents (Int32 start, Int32 last, Int32 direction) (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dSpriteAnimator.cs:709)
tk2dSpriteAnimator.UpdateAnimation (Single deltaTime) (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dSpriteAnimator.cs:658)
tk2dSpriteAnimator.LateUpdate () (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dSpriteAnimator.cs:725)

So I assume that the manual frame jump is what's causing the animator to step off the end of the frame array.  Any thoughts on how I could fix this?

My initial attempt just put the actual freeze trigger on the frame I wanted to use, but that ended up getting skipped unreliably between critters as their animations can be cut short based on their behavior state machine.  Frame 0 seems guaranteed to play before anything potentially switches them up, thus I stuck the trigger there with the "look ahead" value.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Hmm... it feels like this could be solved by calling PlayFromFrame instead of Resume(). How are you setting your frame?

bcullis

  • Newbie
  • *
  • Posts: 3
    • View Profile
You were exactly right, I switched to PlayFromFrame(frozenFrame) and the error went away!

For posterity, this is what it looked like on the "freezing" side:

if(spriteAnimator.CurrentClip.frames[spriteAnimator.CurrentFrame].eventInfo.Equals("HIT_FREEZE"))
{
    _freezeFrame = spriteAnimator.CurrentClip.frames[spriteAnimator.CurrentFrame].eventInt;
    spriteAnimator.SetFrame(_freezeFrame);
    spriteAnimator.Pause();
}

And the original "unfreeze" call was just a spriteAnimator.Resume().  Changing it to spriteAnimator.PlayFromFrame(_freezeFrame) resolved the error.

Thanks!!