-
Notifications
You must be signed in to change notification settings - Fork 23
AbstractTweenable: Taking Tweening to Another Place Altogether
AbstractTweenable is a fantastic example of the flexibility of ZestKit. Sure, it can tween. We've already seen that. ZestKit has some other tricks up it's sleeve as well. The AbstractTweenable class can be subclassed and used for just about anything you can imagine. If you just want to have a look at some real-world examples the CameraShakeTween and TweenChain both happen to be AbstractTweenable subclasses.
Put simply, subclassing AbstractTweenable gives you a tick method that will be called every frame. Out of the box it provides all the ITweenable methods which include start, pause, resume, stop, etc. What you end up with if you subclass AbstractTweenable is a class that can be added to ZestKit (this happens automatically when start is called) and ZestKit will call the tick method every frame until you return true (indicating you are done). Think of it as a coroutine on steroids or something.
Below is an example of the entirety of what it looks like to have a custom class that subclasses AbstractTweenable. The MyKickAssTweenable below can be started by calling start and ZestKit will then call the tick method every frame. If you return false you are telling ZestKit you aren't done yet and tick will continue to be called. When you return true ZestKit will remove the tween from it's internal list and stop calling tick.
That's it. Simple and powerful. Use it for any variable length tasks such as pathfinding, AI, server requests, animations or whatever else you can think up.
public class MyKickAssTweenable : AbstractTweenable
{
public override bool tick()
{
if( _isPaused )
return false;
// Do something cool (return false each tick) and when you're done return true to let ZestKit know
_isActiveTween = false;
return true;
}
}Here is an example of using the MyKickAssTweenable class that we made above. It illustrates some of the features that subclassing AbstractTweenable provides to you out of the box.
// create an instance of MyKickAssTweenable and get it moving
var tweenable = new MyKickAssTweenable();
tweenable.start();
// sometime later...
tweenable.pause();
// after something cool happens...
tweenable.resume();
// ok. thats about enough. Kill the tweenable.
tweenable.stop();
// maybe sometime later you want to get this sucker going again
tweenable.start();
// get the point yet?