Hello Guest

Author Topic: IndexOutOfRangeException while calling animation  (Read 5356 times)

Copywright

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 33
    • View Profile
IndexOutOfRangeException while calling animation
« on: February 03, 2013, 01:22:28 am »
Hey,

I'm trying to get a running animation to play when a key is double tapped. When I double tap the key, I get this error:

Code: [Select]
IndexOutOfRangeException: Array index is out of range.
tk2dAnimatedSprite.Play (Int32 clipId, Single clipStartTime) (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dAnimatedSprite.cs:353)
tk2dAnimatedSprite.Play (Int32 id) (at Assets/TK2DROOT/tk2d/Code/Sprites/tk2dAnimatedSprite.cs:312)
AnimationManager.AnimationChecker (System.String animationName, .tk2dAnimatedSprite anim) (at Assets/Scripts/AnimationManager.cs:62)
AnimationManager.Update () (at Assets/Scripts/AnimationManager.cs:51)

I'm not too sure why, since I'm using the same code for my walking animations, which still work with the code. here's where I call the animations:

Code: [Select]
public class AnimationManager : MonoBehaviour {

public InputManager inputManager;
public tk2dAnimatedSprite animSprite;
public string nameAnimation;


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

if(inputManager.keyName == "Left" && inputManager.hasDoubleTapped == true)
{
nameAnimation = "leftRun";
}
else
{
if(inputManager.keyName == "Right" && inputManager.hasDoubleTapped == true)
{
nameAnimation = "rightRun";
}

}

if( inputManager.keyName == "Down" && inputManager.hasDoubleTapped == true && inputManager.isLookingLeft == true)
nameAnimation = "leftRun";
else
{
if( inputManager.keyName == "Down" && inputManager.hasDoubleTapped == true && inputManager.isLookingRight == true)
nameAnimation = "rightRun";
}

if( inputManager.keyName == "Up" && inputManager.hasDoubleTapped == true && inputManager.isLookingLeft == true)
nameAnimation = "leftRun";
else
{
if( inputManager.keyName == "Up" && inputManager.hasDoubleTapped == true && inputManager.isLookingRight == true)
nameAnimation = "rightRun";
}

if (!animSprite.IsPlaying(nameAnimation))
{
AnimationChecker(nameAnimation, animSprite);
}


}

public void AnimationChecker(string animationName, tk2dAnimatedSprite anim)
{
nameAnimation = animationName;

int clipID = anim.GetClipIdByName(nameAnimation);
anim.Play(clipID);

}

}

It's basically supposed to change the animation name based on whether which key is double tapped. The function was just to make an easy way to change the string into a clip ID, then play it. What's the error with my code that's causing this?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: IndexOutOfRangeException while calling animation
« Reply #1 on: February 03, 2013, 01:28:29 am »
Can you check if clipId is -1 in any case? Perhaps one of your clips is misspelt / doesn't exist?

Copywright

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 33
    • View Profile
Re: IndexOutOfRangeException while calling animation
« Reply #2 on: February 03, 2013, 03:45:54 pm »
Yeah, This is my fault, I'm sorry! I guess I didn't save while making all those new animation clips before closing Unity last time. I remade them, and it works fine now. :) Thanks.