Hello Guest

Author Topic: Make tk2dSpriteAnimator clip play for variable number ofseconds programmatically  (Read 5888 times)

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
I have a cooldown timer sprite animator whose animation clip I want to play for a variable number of seconds. So for example one instance the animation clip will take 60 seconds to complete, or take 10 seconds to complete.

I know of the ClipFps property, and I've been fiddling around with it's value, but I can't seem to figure out what I should be setting it at based on the number of seconds I want the clip to take.

Here's a code sample, maybe there's a better way to do this?
Code: [Select]
// Take 60 seconds to cooldown.
CooldownTime = 60f;
StartCoroutine(CooldownTimer());

private IEnumerator CooldownTimer() {
    cooldownSprite.Play("cooldown");
    cooldownSprite.ClipFps = CooldownTime;
    CooldownFinished = false;
    yield return new WaitForSeconds(CooldownTime);
    cooldownSprite.Play("ready");
    CooldownFinished = true;
}
« Last Edit: January 25, 2014, 07:25:21 pm by Neeko »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
No that is probably the best way to do it. The clip fps will change the rate the animation is playing, I don't think thats what you want?

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
I do want to change the rate the animation clip is playing; the sprite is simply a circle with an animation clip that fills itself in of the course of 13 frames. Sometimes I'll want the circle to fill in a faster rate (example, take 10 seconds to complete the animation), other times at a slower rate (60 seconds to complete the animation). 
« Last Edit: January 26, 2014, 02:28:43 am by Neeko »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
The coroutine is the best way to do this.

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
Right, but the issue I'm having is with this line

Code: [Select]
cooldownSprite.ClipFps = CooldownTime;
If CooldownTime is n, as to represent n seconds, what should I set ClipFps to so that the cooldownSprite animation clip also takes n seconds to complete?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
No, that will be how many frames per second. Setting it to N will mean it will play N frames in one second. You need to check the number of frames in the clip, then work out fps based on that. So number of frames / number of seconds.

Neeko

  • 2D Toolkit
  • Jr. Member
  • *
  • Posts: 59
    • View Profile
    • Overdeveloped
Ah, perfect. Kind of obvious, embarrassed I didn't figure that out myself  :-[

Thanks again!