Prebuilt classes in ActionScript provide a way to access various types of features and objects within the SWF application. The Tween, TransitionManager, and easing classes provide a simple way to add animations and transitions dynamically to your movie. The process for using these classes is very similar to the ActionScript 2.0 versions. The main difference between the ActionScript 2.0 and the ActionScript 3.0 classes is that the ActionScript package is now "fl" instead of "mx" as it appeared previously.
This section provides an overview of the tween, transition, and easing options available in ActionScript 3.0. You'll start by looking at the basics of code-generated tweening.
Note: The Tween and TransitionManager classes also exist in the ActionScript 2.0 language in the "mx" component package. For more information, read About the Tween and TransitionManager classes in the ActionScript 2.0 documentation.
If you're working with the Tween or transition classes, you'll start your code off by importing the transitions package (as well as the easing package if you're planning on using easing). Add this code to the top of the Script window in the Actions panel:
import fl.transitions.*; import fl.transitions.easing.*;
An ActionScript package is a group of ActionScript classes that are related. By importing the transitions and easing packages at the start of your script, you can access the object classes within the package directly by their class name to save horizontal space. Importing the class also instructs Flash CS4 Professional to compile the classes along with the SWF file.
Tip: If you're new to writing ActionScript, know that there is a lot of copying and pasting involved when you're getting started. When you see a useful code snippet, copy and paste it into your file and type into the text to create your script.
Transitions are animations that can be applied to movie clips to make them transition "in" or "out" of the screen. ActionScript 3.0 includes 10 different transition classes, which you can customize using the easing methods, and several optional parameters.
Flash CS4 Professional includes the following transitions for use with the TransitionManager class:
Each transition has slightly different customizations that you can apply to the animation.
The TransitionManager class is the manager object that you use to interact with the transitions.
The following code shows how to create a Wipe transition:
import fl.transitions.*;
import fl.transitions.easing.*;
// Apply wipe transition
TransitionManager.start(img1_mc, {type:Wipe, direction:Transition.IN, duration:2, easing:None.easeNone, startPoint:1});The code above assumes that there is a movie clip instance named "img1_mc" on the Stage. These few lines of code are all that is needed to create a wipe effect in your movie.
Notice that you pass in the instance of the target and an object containing transition parameters. The parameters always include the type, direction, duration, and easing properties—along with any other transition specific properties required.
See the TransitionManager class section of the ActionScript 3.0 Language and Components Reference for more information.
Easing refers to gradual acceleration or deceleration during an animation. For example, a ball might gradually increase its speed near the beginning of an animation, but slow down right at the end of the animation before it arrives at a full stop. Adding easing helps your animations appear more realistic.
Flash offers six easing classes for use with the Tween and TransitionManager classes:
The previous six easing classes each have three easing methods:
Note: The None class contains the easeNone method, as seen in the TransitionManager code example above.
If you want to open these classes in Flash or read the code in your ActionScript editor, you can find the class files in Adobe\Adobe Flash CS4\Common\Configuration\ActionScript 3.0\projects\Flash\src\fl\transitions.
See the fl.transitions.easing package section of the ActionScript 3.0 Language and Component Reference for more information on the TransitionManager class.
The Tween class enables you to move, resize, and fade movie clips easily on the Stage, similar to a tween built along the Timeline.
For example, imagine that you want to move a movie clip across the Stage. Your options would include:
onEnterFrame event handler to power the animationsetInterval() function to call a function at periodic intervalsOne of the easiest ways to create a tweened animation is to use the Tween class. Use the following ActionScript:
import fl.transitions.*; import fl.transitions.easing.*; var myTween:Tween = new Tween(ball_mc, "x", Elastic.easeOut, 0, 300, 3, true);
The previous ActionScript code example creates a new instance of the Tween
class, which animates the ball_mc movie clip on the Stage along the x axis (left to right). The movie clip
animates from 0 pixels to 300 pixels in three seconds, and the ActionScript
applies an elastic easing method. This means the ball extends past 300
pixels on the x axis before animating back using a fluid motion effect.
The following list shows the parameters in the Tween constructor, Tween(obj, prop, func, begin, finish, duration, useSeconds):
infinity if the value is negative or omittedThe first code example above demonstrates the settings to use to set the
duration of the animation in seconds. Instead of using seconds, you can fade
the symbol over a few frames. To set the duration in frames instead of seconds
in the Tween class, you change the final parameter, useSeconds, from true to false. When you set the parameter to true, you tell Flash that the specified
duration is measured in seconds. Or, if you set the parameter to false, the duration is the number of
frames you want to use for the tween. For example, let's examine the following
code:
import fl.transitions.Tween; import fl.transitions.easing.*; var myTween:Tween = new Tween(ball_mc, "alpha", Strong.easeIn, 1, 0, 24, false);
The previous code example fades out the ball_mc instance using the Strong.easeIn easing method. Instead of fading the instance for three seconds, it fades the
instance across 24 frames. Using frames instead of seconds offers you a lot
more flexibility, but remember that the duration is tied to the frame rate of
the current Flash document. If your Flash document uses a frame rate of 12
frames per second (fps), then the previous code snippet fades the instance over
two seconds (24 frames/12 fps = 2 seconds). However, if your frame rate is 24
fps, then the same code fades the instance over one second (24 frames/24 fps =
1 second). If you use frames to measure duration, you can significantly change
the speed of your animation when you change the document's frame rate, without
modifying your ActionScript.
Read about the Tween class in the ActionScript 3.0 Language and Components Reference for more information.