Hello Guest

Author Topic: check on animation  (Read 4702 times)

peterbrinson

  • Newbie
  • *
  • Posts: 9
    • View Profile
check on animation
« 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");
}

guidez

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: check on animation
« Reply #1 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");
}

peterbrinson

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: check on animation
« Reply #2 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

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: check on animation
« Reply #3 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 );