Dynamically generating tweened animations with actionscript is wonderful. Here is an example of code for an object that fades out over 0.5 seconds.
----- excerpt from custom class IndicatorLine.as -----
_tween = new Tween(
this, // object to change property of
"alpha",// property to change
Regular.easeIn,// ease formula
1, // from
0, // to
0.5, // duration
true // use seconds for duration
);
One hurdle that tied me up for a while was the inability to coordinate an abstract property or several properties at once, such as the height of a bouncing ball or the status of a dynamic animation.
I found the solution. Here is a demonstration of the effect.
download source files: firecrow_as3_bouncing_ball_example.zip
The ball is beeing tweened on two levels at once. The first beeing the x position for the up and down motion of the bounce, and the second beeing the bounce height which smoothely transitions to match the value of the slider.
I found the solution when, out of curiosity, I read the source code for the fl.transitions.Tween package. The solution is simple, the property is dynamically assigned as a string, which means any get/set method can be used.
----- excerpt from Flash CS3 lib fl.transitions.Tween -----
public class Tween extends EventDispatcher
{
...
/**
* constructor function sets up an on EnterFrameListener
* which calls "setPosition" to increment the property (prop)
* util it reaches the specified value
*/
function Tween(obj:Object, prop:String,...)
{
...
public function setPosition(p:Number):void
{
...
// p is the value of the next frame for prop
this.obj[this.prop] = p;
...
}
}
}
Getter and setter are my new favorite thing for making methods that can be called like a property.
----- excerpt from custom class Bounce.as -----
public function get distance():Number
{
return _distance;
}
public function set distance(distance:Number):void
{
_distance = distance;
}
/**
* note: in this case the set mothod is not really necessary
* but it can be a great source of adding behaviour to the
* setting of a property, such as to redraw the object
* when a property is changed
*/
And then that property name can be fed into the tween.
----- excerpt from custom class Stage.as -----
_bounceDistanceTween = new Tween(
...
"distance", // property to adjust on object
...
);
The effect is that the Tween instance will update the property calling the set method with each value for a frame during the specified duration.
----- sample code -----
/**
* as if this is beeing excecuted
*/
bounce.distance = value