Hello Guest

Author Topic: Get the current frame playing?  (Read 8867 times)

regnared

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
    • Mah website
Get the current frame playing?
« on: March 14, 2013, 08:37:54 pm »
How do you go about at acquiring the current frame being played?  Here is my problem :



I am using frames for the tongue bit going in a curve, and the handle animation. When you press UP or DOWN, the animation plays, then you don't press anything it stops. For the tongue curve part, I simply do Anim.Pause(); And when you press a key it does   Anim.Play("TongueCircleUp") or Anim.Play("TongueCircleDown").

When you use Play() it restarts to the first frame. So in my case, the best thing would be to use PlayFromFrame (int frame) So that there is no animation skip. But I am not certain how to acquire the frame that is currently being played.

Is there a function to acquire that data? It would be highly useful.

Also, is there a way to make the animation play until the last frame in the clip? Say, Play the animation once, then stop. This is mainly due for the handle animation, since the little guy jumps, it looks odd with him in mid-air. I guess if I could acquire the frames playing, I could do play until last frame then pause(); Two birds, one stone deal? :)

Thanks in advance.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get the current frame playing?
« Reply #1 on: March 14, 2013, 09:00:15 pm »
You can't get the current frame directly, but you can get the current clip time, and use that to play with the other clip.
float t = anim.ClipTimeSeconds;
anim.Play( "newclip", t); // will start off this new clip at the same clip time.

If you really still need the frame  you can acquire it, but it will only work directly for looped & one shot clips.

You can set the animation loop mode to once, that will play it once and stop?

regnared

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
    • Mah website
Re: Get the current frame playing?
« Reply #2 on: March 15, 2013, 12:34:51 am »
Quote
You can't get the current frame directly, but you can get the current clip time, and use that to play with the other clip.

For my case I tried it and it doesn't work. The DOWN animation uses the same animation frames as UP, but reversed. So if I get the current clip time, the frames do not match. Like this :



So if I say Play("TongueCircleUp", 0.03) it will start at frame 0016 , and Play("TongueCircleDown", 0.03) it will start at frame 0122. What I need is for both animations to start at the same frame.

How would I go to acquire the current frame of a clip?

Quote
You can set the animation loop mode to once, that will play it once and stop?

Can I change the animation loop directly in a tk2dAnimatedSprite using code? I haven't found a way to do it inside the documentation page. I ask this, because I would like to make the animation to play in a loop mode when you press a key, and once you stop pressing a key, the wrap mode goes to once and stop. Would that be possible?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get the current frame playing?
« Reply #3 on: March 15, 2013, 12:59:19 am »
You can get the current frame using this:
(anim.ClipTimeSeconds * anim.ClipFps) % anim.Clip.frames.Length; // for a looped animation
Refer to the EditorClipTime function for definitions for other types of animation. You could possibly uncomment that and use it if you wanted too, though that is designed for the editor (does some funny stuff with pingpong / loopsection to make it look presentable).

You could change it in code if you wanted to, but I strongly suggest creating a copy of the tk2dSpriteAnimationClip, and Playing using that directly. Then you can fiddle to your hearts content. Even change frames should you desire.

regnared

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 27
    • View Profile
    • Mah website
Re: Get the current frame playing?
« Reply #4 on: March 15, 2013, 02:56:43 am »
Wow, a big thank you for all the information you've given me. That totally was what I needed. I finally managed (somehow) to get it to work! :D

I had to calculate which frame corresponded from the GoingUp animation and the GoingDown one. Using a little bit of math it worked. I guess it would of also worked using the current time of the animation . I'll prob make an extension method for this to implement it in an elegant way. Cheers. :)

Evil-Dog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Get the current frame playing?
« Reply #5 on: January 09, 2015, 05:17:35 pm »
I couldn't find a confirmation anywhere but am I correct in assuming that spriteAnimator.CurrentFrame is the same as doing (anim.ClipTimeSeconds * anim.ClipFps) % anim.Clip.frames.Length ?  Just say CurrentFrame in the doc in v2.5
Thanks for confirming.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get the current frame playing?
« Reply #6 on: January 09, 2015, 05:55:26 pm »
Kinda, depends on the clip type, its not the same if its pingpong for instance..

Evil-Dog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Get the current frame playing?
« Reply #7 on: January 09, 2015, 06:04:14 pm »
Do you mean that in ping pong, CurrentFrame will go back and forth but using ClipTime will just go forward?  I'd need to make some tests but I think the doc should explain that stuff maybe. There's no doc on wrap modes that I can see.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get the current frame playing?
« Reply #8 on: January 09, 2015, 06:07:27 pm »
Yes, cliptime always goes forward, CurrentFrame will be the current frame regardless of time. The wrap modes just followed the unity ones, so didn't bother documenting that. Easiest thing to do is to look at CurrentFrame code, should be self explanatory (I hope)

Evil-Dog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Get the current frame playing?
« Reply #9 on: January 10, 2015, 04:38:35 am »
Well yeah but when using a third party, I don't expect I have to go dig in the code to understand stuff hehe should be in the docs :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get the current frame playing?
« Reply #10 on: January 10, 2015, 09:44:24 am »
http://2dtoolkit.com/docs/latest/reference/sprite_animation.html
Tells you what the wrap modes do high level, Has always done so. Codes there for actual implementation details

Evil-Dog

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Get the current frame playing?
« Reply #11 on: January 10, 2015, 12:25:22 pm »
Sure, thanks for the info :)