Hello Guest

Author Topic: Changing wrap mode while playing  (Read 7467 times)

VGT

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Changing wrap mode while playing
« on: July 09, 2012, 06:20:31 pm »
I have an animation loop that when I decide for it to stop, I'd like it to finish before going onto the next sequence.  I currently call on GetComponent<tk2dAnimatedSprite>().animation.wrapMode = WrapMode.Once.

This doesn't seem to have an effect.  Is it possible to make this work through some other means?

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing wrap mode while playing
« Reply #1 on: July 09, 2012, 10:19:57 pm »
This is probably not going to work straight off. The internal counters would have overflowed if you switch the wrap mode at runtime. There are a few different ways to do this. I'll describe one possible way.

You set a trigger on the last frame of your animation.
In an attached behaviour, attach an animationEventDelegate and in the function, do something like this....

Code: [Select]
void animationEventDelegate(...)
{
   if (stopAfterLoop == true)
   {
      var clip = animatedSprite.CurrentClip;
      var lastFrame = clip.frames[clip.frames.Length - 1];
      animatedSprite.Stop(); // stop the animated sprite
      animatedSprite.SwitchCollectionAndSprite(lastFrame.spriteCollection, lastFrame.spriteId);
   }
}

Instead of changing the clip, you simply set stopAfterLoop = true, and this will stop the animation after the current iteration.

The reason the SwitchCollectionAndSprite line is required is that the trigger could have fired a few animation frames ago (depending on the speed of your animation), and the currently displayed frame could be a different one.

Ideally all you'd need to do is do animatedSprite.Stop(); animatedSprite.SetFrame(frameNum); but with the current API, it'll trigger the event recursively will be really bad!

There were a few issues with calling stop from a callback in one of the previous versions. All of these have been fixed (to the best of my knowledge) in 1.76b1, so drop me an email if you run into any issues and let me know the version you're on - I should be able to sort out a patch.


« Last Edit: July 10, 2012, 12:25:49 am by unikron »

VGT

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Changing wrap mode while playing
« Reply #2 on: July 10, 2012, 06:26:52 pm »
Thank you for the information, that did the trick! 

I do have one concern.  I have come to find out that the animation triggers are un-reliable.  I haven't found a way to re-produce it yet but animations will sometimes not call the Animation Event Delegate that has been assigned to it despite going through that frame.  This will happen with animations that are running at 24fps and 30fps. 

For some of my events I have resorted to making it time based since I have functionality that is too dependant on reaching a trigger.

This is using Unity 3.5.1 and 2D Toolkit version 1.7 final + patch2
« Last Edit: July 10, 2012, 06:31:47 pm by VGT »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing wrap mode while playing
« Reply #3 on: July 10, 2012, 09:01:24 pm »
The problem with the triggers has been fixed in 1.75/6. I suggest waiting for 1.76 final if you are thinking of upgrading, there were a few bugs in 1.75 which have been fixed in there. If you want I can send you a local patch to just the animated sprite stuff which will sort this out for you in the version you're on. Let me know.

VGT

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Changing wrap mode while playing
« Reply #4 on: July 10, 2012, 10:47:21 pm »
For now I would like the fix to that specific problem.  I'll try just updating the one cs file to see if that's possible for now.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Changing wrap mode while playing
« Reply #5 on: July 10, 2012, 11:10:33 pm »
Please drop me an email to remind me - I'll do this when I get on tomorrow. I'll need to check it actually works in that version before sending it to you :)