2D Toolkit Forum

2D Toolkit => Support => Topic started by: peterbrinson on June 27, 2013, 07:33:42 pm

Title: check on animation
Post by: peterbrinson on June 27, 2013, 07:33:42 pm
I play my animations happily with

anim.Play(theAnim);

...where 'theAnim' is a string I concatenate from a few different variables.  If 'theAnim' turns out not to be a proper animation, it breaks the game.  So yes, I agree that I should fix any such mistakes. 

But I want to make game breaking bugs impossible, so is there a way to check on the animation before I play it, like:

if(--check 'theAnim' exists---){
     anim.Play(theAnim);
}else{
     anim.Play("dummySprite");
}
Title: Re: check on animation
Post by: guidez on June 27, 2013, 08:33:30 pm
See if animation is playing: http://answers.unity3d.com/questions/14599/check-if-a-spesific-animation-is-playing.html

If you just want to make sure "theAnim" is not null, then:

Code: [Select]
if(theAnim){
     anim.Play(theAnim);
}else{
     anim.Play("dummySprite");
}

It's the same as saying

Code: [Select]
if(theAnim != null){
     anim.Play(theAnim);
}else{
     anim.Play("dummySprite");
}
Title: Re: check on animation
Post by: peterbrinson on June 27, 2013, 09:09:04 pm
'theAnim' is just a string.  But you made me realize that something like this works fine

if(anim.GetClipByName(theAnim) != null)
   anim.Play(theAnim);


thanks
Title: Re: check on animation
Post by: unikronsoftware on June 27, 2013, 10:42:48 pm
@ peterbrinson that is the right way to do it. You can optimize it a tiny bit further by doing this:
Code: [Select]
tk2dSpriteAnimationClip clip = anim.GetClipByName( theAnim );
if (clip != null)
    anim.Play( clip );