Escolar Documentos
Profissional Documentos
Cultura Documentos
com/tutorials/flash/as3tweenclass/
Basic Usage of the Tween Class
The Tween Class is a ActionScript class that can be used to tween any property of any object. By the
term tween we mean here the gradual increase or decrease of the value of a property which can bring
about an animation. This could be the increase of transparency, the decrease in width, or the movement
from left to right of any object.
The Tween Class is not included by default in a Flash movie and must be imported before it can be
used, Adobe did this to ensure to help making SWF files smaller so that the Class is included only in
movies that require it. To import this class you must use the import directive at the beginning before
you start your actual code. Here is the code you need:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
The Tween Class must be used through an instance of it. When instantiating the Tween Class you must
provide the parameters required by the TWeen Class. This process is done by the manner illustrated in
the following generalized code:
var myTween:Tween = new Tween(object, "property", EasingType, begin, end, duration,
useSeconds);
All of these parameters must be provided when the class is instantiated. Here is what these parameters
mean:
object - This is the instance name of the object which will be animated. Example: my_box, gallery_mc,
myTextField_txt.
property - This is the name of the property which will be animated, it must be specified as a string
(between quotation marks). Example: "alpha", "scaleX", "scaleY", "x", "y".
EasingType - The easing type will determine how the tween animation will flow. We are going to
explain this more in the next section of the tutorial. Examples: Strong.EaseIn, Elastic.EaseOut,
Back.EaseInOut.
begin - This is the position from which the animation will start, it must be specified as a number.
end - This is the position at which the animation will stop, it must be specified at as a number.
duration - This is the period for which the animation will run, the default unit for it is frames, however,
the unit can be set in seconds if you set the next parameter as true.
useSeconds - Set this parameter to true if you want to the duration to be measured in seconds instead of
frames.
So for example, if we want to move an object called my_box along the x axis, from position 40 to
position 300 in an animation that takes 5 seconds we would write it this way:
var myTween:Tween = new Tween(my_box, "x", Strong.easeOut, 40, 300, 5, true);
I intentionally did not talk about the EasingType because we will cover that in the next section below.
Running your own example
Create a new AS3 Flash movie, draw a square or any other shape, convert it to Movie Clip symbol
(press F8 to do this) and assign the instance name my_box to it using the Properties Inspector. You will
then have to right-click the only frame on the timeline and access the Actions panel. Paste the code
below and test your movie (Ctrl+Enter) to see it move:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var myTween:Tween = new Tween(my_box, "x", Strong.easeOut, 40, 300, 5, true);
You can animate different properties and try out different values by changing the parameters. For
example to make your object fade out make alpha decrease from 1 to zero in 10 seconds:
var myTween:Tween = new Tween(my_box, "alpha", Strong.easeOut, 1, 0, 10, true);
Using Easing Classes to Alter Tween Motion
It is possible to alter the motion flow of the tween by using different easing classes. Easing is the
acceleration or the deceleration of the speed at which the object is moving. It is quite hard to explain in
words, but it is easy to understand by just looking at it (Check the example below).
There are basically six easing types:
Regular: the motion speed will gradually increase or decrease in speed as specified by the easing
method.
Bounce: the motion will bounce back a few steps when it reaches the end position before settling at it.
Back: the motion will go beyond the end position before bouncing back into it.
Elastic: a mixture of Bounce and Back effects combined.
instance with the MOTION_FINISH event and executes the .yoyo() method when it's done. This
effectively makes the tween loop forever.
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var myTween = new Tween(my_mc, "x", Strong.easeInOut, 100,300, 1, true);
myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
myTween.yoyo();
}
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var myTween = new Tween(my_mc, "x", Strong.easeInOut, 100,300, 1, true);
myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
e.target.yoyo();
}
This can let the event listener be used with more than one tween at the same time as it does not
explicitly refer to an actual tween - a good idea for saving time and making things more organized.
Misc Tween Properties
In this final short section of the tutorial I will tell you about two properties of the Tween Class which
can be accessed to retrieve information you might need. Both of these properties must be accessed
directly from the instance of the Tween Class. Here they are:
obj - is a reference to the object currently being tweened. Using this property is helpful if you need to
refer to it later using an event handler. The example below will set the alpha property to .5 when
the tween finishes:import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var myTween = new Tween(my_mc, "x", Strong.easeInOut, 100,300, 1, true);
myTween.addEventListener(TweenEvent.MOTION_FINISH, onFinish);
function onFinish(e:TweenEvent):void {
myTween.obj.alpha=0.5;
}
position - is a reference to the current position of the tween. This property is usually accessed through
out the motion of the tween. The example below outputs the current position in the output
window when the movie is tested:import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
var myTween = new Tween(my_mc, "x", Strong.easeInOut, 100,300, 1, true);
myTween.addEventListener(TweenEvent.MOTION_CHANGE, onChange);
function onChange(e:TweenEvent):void {
trace(myTween.position);
}