Hello Guest

Author Topic: Get current frame?  (Read 8421 times)

VeTaL

  • Newbie
  • *
  • Posts: 18
    • View Profile
Get current frame?
« on: May 12, 2012, 07:51:28 am »
Looked through reference for AnimatedSprite, found void tk2dAnimatedSprite.SetFrame(int currFrame   )   
http://unikronsoftware.com/2dtoolkit/doc/html/classtk2d_animated_sprite.html

But how to get current frame? For example, i need forward and backward animation. How can i reverse it?

Something like
Code: [Select]
if (Input.GetMouseButtonDown(0))
{
frameCurrent = anim.GetFrame();
anim.PlayFromFrame("Forward",frameCurrent);
}
if (Input.GetMouseButtonDown(1))
{
frameCurrent = anim.GetFrame();
anim.PlayFromFrame("Reverse",frameCurrent );
}

Kinda this:
Left Mouse Button - 0-1-2-3-4-5- Right Mouse Button - 5-4-3-2-1-0-30-29-28-27 - Left Mouse Button - 27-28-29-30-0-1-2- ...

PS: ideally, i think something like field Reverse should do all this stuff.
« Last Edit: May 12, 2012, 08:32:06 am by VeTaL »

VeTaL

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Get current frame?
« Reply #1 on: May 12, 2012, 08:55:51 am »
Not sure if this is supposed way of solving this problem, but minor changes in tk2dAnimatedSprite.cs do the work.

Just added to definition section
public int currFrame = 0;

and removed "int"s before all currFrame in void Update ()

VeTaL

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Get current frame?
« Reply #2 on: May 12, 2012, 09:25:42 am »
Moreover, if anybody would need isReversed flag - here it is:

Code: [Select]
public class tk2dAnimatedSprite : tk2dSprite
{
public int currFrame = 0;
public bool isReversed = false;

********

void Update ()
{
#if UNITY_EDITOR
// Don't play animations when not in play mode
if (!Application.isPlaying)
return;
#endif

if (g_paused || paused)
return;

if (currentClip != null && currentClip.frames != null)
{
clipTime += Time.deltaTime * currentClip.fps * ((isReversed)?-1:1); // modified

if (currentClip.wrapMode == tk2dSpriteAnimationClip.WrapMode.Loop || currentClip.wrapMode == tk2dSpriteAnimationClip.WrapMode.RandomLoop)
{
currFrame = (int)clipTime % currentClip.frames.Length;
if (currFrame <0) currFrame += currentClip.frames.Length; // added
SetFrameInternal(currFrame);
}
else if (currentClip.wrapMode == tk2dSpriteAnimationClip.WrapMode.LoopSection)
{
currFrame = (int)clipTime;
if (currFrame >= currentClip.loopStart)
{
currFrame = currentClip.loopStart + ((currFrame - currentClip.loopStart) % (currentClip.frames.Length - currentClip.loopStart));
}
if (currFrame <0) currFrame += currentClip.frames.Length; // added
SetFrameInternal(currFrame);
}
else if (currentClip.wrapMode == tk2dSpriteAnimationClip.WrapMode.PingPong)
{
currFrame = (int)clipTime % (currentClip.frames.Length + currentClip.frames.Length - 2);
if (currFrame >= currentClip.frames.Length)
{
int i = currFrame - currentClip.frames.Length;
currFrame = currentClip.frames.Length - 2 - i;
}
if (currFrame <0) currFrame += currentClip.frames.Length; // added
SetFrameInternal(currFrame);
}
else if (currentClip.wrapMode == tk2dSpriteAnimationClip.WrapMode.Once)
{
currFrame = (int)clipTime;
if (currFrame <0) currFrame += currentClip.frames.Length; // added
if (currFrame >= currentClip.frames.Length)
{
currentClip = null;
OnCompleteAnimation();
}
else
{
SetFrameInternal(currFrame);
}

}
}
}

"Loop" mode works fine for me. Though, not sure about other modes (especially about WrapMode.Once). But now it works like this:
Code: [Select]

if (Input.GetMouseButtonDown(0))
{
anim.isReversed = false;
}
if (Input.GetMouseButtonDown(1))
{
anim.isReversed = true;
}

Debug.Log(anim.currFrame);

VeTaL

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Get current frame?
« Reply #3 on: May 12, 2012, 09:51:14 am »
Also, probably, it is nice idea to add
Code: [Select]
public void Play(int id, float clipStartTime)
{
isReversed = false;
****

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Get current frame?
« Reply #4 on: May 12, 2012, 11:44:26 am »
I'll consider a play reversed option when rewriting the animation editor interface. Either in versions 1.8 or 1.9, depending on how the multi res research goes.