Hello Guest

Author Topic: Stop a looping animation  (Read 4455 times)

Staffiend

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Stop a looping animation
« on: November 21, 2013, 10:16:25 pm »
Hey.  I was wondering if there was a way to stop a looping animation.  It starts out just fine, but even when the if statement isn't met, the animation continues to play.  Why is this?  Any help would be greatly appreciated.

Code: [Select]
using UnityEngine;
using System.Collections;

public class BoostAndDodge : MonoBehaviour {

tk2dSpriteAnimator anim;

public float boostSpeed = 5;
public float chargeTimer = 1;
public float waitTimer = 1;
bool executing = false;



IEnumerator WaitForCharge()
{
executing = true;
yield return new WaitForSeconds(chargeTimer);
Energy.ENERGY = 1;
Debug.Log("coroutine complete");
executing = false;
}

IEnumerator Wait()
{
yield return new WaitForSeconds(waitTimer);
Energy.ENERGY = 0;
}

// Use this for initialization
void Start () {

anim = GetComponent<tk2dSpriteAnimator>();

}

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

float boost = boostSpeed * Time.deltaTime;

if(Energy.ENERGY == 1 && Input.GetButton("Jump"))
{
anim.Play("Boost");
transform.Translate(new Vector3(boost, 0, 0));
StartCoroutine(Wait());
Debug.Log("boosted");
}

if(executing == false  && Energy.ENERGY == 0)
{
StartCoroutine(WaitForCharge());
}

}
}

Afro-Ninja

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 62
    • View Profile
Re: Stop a looping animation
« Reply #1 on: November 22, 2013, 08:15:53 am »
anim.Pause(); ?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Stop a looping animation
« Reply #2 on: November 22, 2013, 11:49:01 am »
anim.Stop() ?

Staffiend

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Stop a looping animation
« Reply #3 on: November 26, 2013, 07:05:02 pm »
Sorry, I should have been more clear with my question.  What I really want to do is not just stop a looping animation, but also return to the animation that was playing previously (which is also a looping animation).  I have tried a few different things and the closest I've been is stopping and resuming the animation that was playing previously, but the "Boost" animation I'm playing in between is stuck on one frame.  So instead of going on forever, it doesn't loop at all when it plays and afterwards the previous animation resumes playing normally.  Any suggestions on how to make this work?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Stop a looping animation
« Reply #4 on: November 26, 2013, 11:01:13 pm »
It looks like you're mixing up the coroutine with Update and ones overriding the other. You probably want to use one or the other, but not both. I suggest setting a breakpoint in Update to try and find out whats going on there.

Staffiend

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Stop a looping animation
« Reply #5 on: November 27, 2013, 01:23:40 am »
Thank you for the help.  I took out one of the coroutines and tried a simplified, more common sense approach and it works perfectly.  Turns out I was just over-complicating things.