Embedding JS Script Tags in Wo…
A Meandering Path to an Evolvi…Quick as a FlashThings Every ActionScript Deve…Making Money with Adobe AIRUsing Flash for 3D Game MMO UIUtilizing Flash for Game UI De…Why I (Still) Love FlashMy Favourite Things
Posted on August 18, 2008 by Grant Skinner
gTween: Programmatic Tweening for AS3 Developers
There are a lot of great tweening engines out there. Personally, I’m a huge fan of Jack Doyle’s TweenLite (and he’s been incredibly productive adding new features lately). However, none of these libraries exactly fit the needs of me and my team. I wanted an engine that was small, fast, hugely flexible, and built from the ground up for AS3 and developers.
The result was gTween. gTween is a small (4.5kb), fast (1500 instances, 0.5s duration, ~25fps), instance based tweening class, with a huge number of options and capabilities. You can read about core features of gTween below.
This is certainly not an attempt to displace any of the existing tween engines. Rather, it’s simply an attempt to share another option for managing programmatic tweens with the developer community.
For now, I am releasing this as a public beta. It has been loosely tested, but there is so much capability in gTween that I guarantee there are still bugs and edge cases we haven’t addressed. Likewise, I am soliciting input on additions or modifications to the API (including the names of specific properties or methods), so it is possible that the API may change in the future.
Quick Feature Overview
Developer Oriented (consistent AS3 experience)
gTween is built for ActionScript 3 developers from the ground up. It uses a more conventional instance-oriented model, rather than a static interface. It also leverages the AS3 event model instead of callbacks (see Events). This provides a familiar, consistent experience for AS3 developers to work with tweens.
Here’s a simple example of setting up a tween, and listening for the complete event.
Instances can either be referenced or transient. The former allows for changing values and behaviour on the fly (see Sequencing), and the latter works with smart resource management logic to ensure the tween won’t be collected until it is finished running (see Resource Management).
Examples of referenced versus transient instances:.
gTween will also work with any numeric property of any object. You can use it to tween the volume of a sound, the size of an AIR window, or the value of “foo” on a custom object type, as easily as you would tween the x property of a Sprite.
gTween provides a unique proxy property that allows you to work with tween destination values the same way you would work with them on the target object. The proxy object effectively “stands in” for your target object.
This simple example shows how you can use the proxy to move the target object 100 pixels to the right.
In the above example, the proxy will retrieve the x value from the target object (if it doesn’t already exist as a destination value in the tween), add 100, and set it back as a destination value for the tween.
This is a very powerful feature that takes a little getting used to. Take a look at the proxy listing in the full API documentation for more information and examples.
Interrupting Tweens (make changes on the fly)
With gTween, you can modify your tween while it is playing, and it will attempt to accommodate those changes.
The user closes a panel before its done sliding in? Just reverse() it, and it will slide back closed from its current position. Need to move a sprite to a different location, or add an alpha fade out while the tween is running? Just adjust the values on the fly.
Here’s a simple example showing how you can tween a sprite to the location a user clicks on stage. Note that the user can click at any time, and the tween will adjust itself to smoothly reach the destination.
You can also jump to the end or beginning of a tween instantly with beginning() or end(). Calling end(true) will recursively end sequenced tweens.
Sequencing Tweens (child tweens, and nextTween)
gTween provides three features to allow for sequencing multiple tweens: child tweens, delay, and nextTween.
By using addChild, you can associate multiple tweens to act as children of your tween. These tweens will be synchronized with the parent tween when it runs. You can even choose to synchronize their delay, beginning, ending, or duration.
You can also specify another tween to play when a tween ends using nextTween, and specify a delay period that a tween will wait before playing.
The following example demonstrates playing two tween synchronously, followed by a 2 second delay before a third tween plays.
Timing Modes (frame, time, or hybrid)
Another unique feature of gTween is support for multiple timing modes. You can use the common time based timing, frame based timing, or a hybrid timing model.
In the “time” timing mode, all durations and positions are specified in seconds, and updates are run at a configurable frequency (ex. every 50ms). This provides tight control over durations, but can use more CPU, and be a bit more choppy in high CPU scenarios.
Using the “frame” timing mode, all durations and positions are specified in frames, and updates are run each frame. This uses a bit less CPU, and allows you to synchronize your tween with timeline animations, but the overall time duration can be unpredictable in high CPU scenarios.
The “hybrid” mode updates each frame, but allows you to specify durations in seconds. Each frame the tween will adjust its position according to the elapsed time since the previous update.
Because it is unlikely developers will change modes during a project, it is specified as a class level property. However, gTween will continue to use the old setting with old tweens, which allows you to change the mode at any time.
Being able to pause all tweens can be handy. Pause all of the sprites in your game when the user pauses it, or pause all the active transitions when you open a dialog.
gTween offers a class level pauseAll property that will pause all tweens, without affecting their per instance paused property. Handy!
gTween offers three built in events: an activate event that lets you know when a tween starts running, an init event that fires when the tween finishes delaying and starts tweening, a change event that fires each time the tween updates, and a complete event that fires when it reaches its end.
Not enough? gTween allows you to dynamically add custom progress points to your tween that will fire when the tween reaches that point.
The following example shows how you can receive an event when a 3 second tween passes the halfway point (1.5 seconds), and get back a “halfway” string:
Note that you can associate any type of data with a progress point. A string labelling the point, a function to call when you reach that point, another tween to run, anything you want.
Resource management is important. You don’t want old tweens hanging around using CPU and memory. You also don’t want unreferenced tweens being garbage collected while they are still active.
Besides being developed with the AVM2 garbage collector in mind, gTween also uses a couple of strategies to allow unreferenced tweens to be collected when they are no longer needed, while preventing them from being destroyed when they are needed.
gTween has been tested creating 3000 new tween instances per second with no memory leaks.
gTween has a lot of additional features. I’m not going to write about all of them, but here are a few:
To access the API documentation, and download the latest build of GTween, visit the GTween page at gskinner.com/libraries/gtween/.
Please provide any feedback or bug reports in the comments below.
Sep 3, 2008: Beta 2 has been released. Learn more about it by clicking here.
Nov 4, 2008: Beta 3 has been released. Learn more about it by clicking here.
Dec 15, 2008: Beta 4 has been released. Learn more about it by clicking here.
Follow @gskinner on Twitter for more news and views on interactive media.