2D Toolkit Forum

2D Toolkit => Support => Topic started by: cypher123 on July 22, 2013, 07:18:54 pm

Title: help with stopping animation/checking when done
Post by: cypher123 on July 22, 2013, 07:18:54 pm
So, I have 2 animations attack and idle, idle is a loop and attack is set to once. I have the sprite with the sprite animator and this code below attached to the sprite. The animations work well the idle loops, but when I set attack to true the attack animation plays forever even though it is set to once. It seems AnimationCompleted  never gets hit, am I doing something wrong with AnimationCompleted? I want the animation to just play once then go back to idle.


Code: [Select]
public bool attack { get; set; }

private tk2dSpriteAnimator anim;

void Start() {
anim = GetComponent<tk2dSpriteAnimator>();

attack = false;
}

void PlayIdle(tk2dSpriteAnimator sprite, tk2dSpriteAnimationClip clip) {

anim.Play("idle");
}



void Update() {

if (attack) {

if (!anim.IsPlaying("attack")) {
anim.Play("attack");

anim.AnimationCompleted = PlayIdle;
}
}else{
if (!anim.IsPlaying("idle")) {


anim.Play("idle");

anim.AnimationCompleted = null;

}
}
}
Title: Re: help with stopping animation/checking when done
Post by: unikronsoftware on July 22, 2013, 08:01:11 pm
When is attack set, and when is it reset to false? It doesn't look like its getting reset in your code - it may be calling Play("attack") again immediately after? Its impossible to work this outside the context you're using it in - you'll have to spew a bunch of Debug.Logs to see whats happening, where and why.
Title: Re: help with stopping animation/checking when done
Post by: cypher123 on July 22, 2013, 08:39:20 pm
I've tried dozen's of different ways with settings attack to true. sorry I had attack = false; in both the PlayIdle function and if anim.Play("idle");. i've set a bunch of debug.logs("") around and PlayIdle never gets hit ever for animation complete. Also I tried setting the idle to play once and it plays endlessly as well. Also tried just doing anim.Play("attack"); in the Start() and it plays attack endlessly there as well. Can you give me any advice on how to go about doing this? Because this obviously isn't working. Attack just stays true, is there another way to check if an animation is complete?
Title: Re: help with stopping animation/checking when done
Post by: unikronsoftware on July 22, 2013, 09:57:32 pm
Are you sure the attack animation is set to once and not loop? Try debug.Logging the wrapMode on CurrentClip after playing it...
Title: Re: help with stopping animation/checking when done
Post by: cypher123 on July 23, 2013, 02:32:24 pm
yea it is saying loop, even though looking in the sprite animation it says once for both idle and attack.
Title: Re: help with stopping animation/checking when done
Post by: unikronsoftware on July 23, 2013, 02:35:37 pm
Something must be going wrong somewhere - it uses the objects as is. Can you send me a stripped down project to look at? support at unikronsoftware.com
Title: Re: help with stopping animation/checking when done
Post by: cypher123 on July 23, 2013, 03:59:46 pm
AHHH, I found the problem. I had duplicate file names for the collection and animation and the duplicates had it set to loop and they were hidden deep in my project and named the same as the current collection/animations thanks so much for your help! I've honestly been confused for the past 2 days over this....