2D Toolkit Forum
2D Toolkit => Support => Topic started by: VeTaL 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
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.
-
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 ()
-
Moreover, if anybody would need isReversed flag - here it is:
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:
if (Input.GetMouseButtonDown(0))
{
anim.isReversed = false;
}
if (Input.GetMouseButtonDown(1))
{
anim.isReversed = true;
}
Debug.Log(anim.currFrame);
-
Also, probably, it is nice idea to add
public void Play(int id, float clipStartTime)
{
isReversed = false;
****
-
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.