By tweening shapes, you can create an effect similar to morphing, making one shape appear to change into another shape over time. Flash can also tween the location, size, color, and opacity of shapes.
Tweening one shape at a time usually yields the best results. If you tween multiple shapes at one time, and want them to morph together, all the shapes must be on the same layer. Otherwise, for some effects, you should shape tween each shape on separate layers if you do not want them to affect each other. Each rectangle‹the curtain effect in the background and the rectangle on top tweens on its own separate layer.
To apply shape tweening to groups, instances, or bitmap images, you must first break these elements apart (Modify > Break Apart). To apply shape tweening to text, you must break the text apart twice to convert the text to objects.
Note: To control more complex or improbable shape changes, you use shape hints, which control how parts of the original shape move into the new shape. For information, see the following section, Using Shape Hints with Shape Tweens.
To tween a shape:
Using Shape Hints with Shape Tweens
To control more complex or improbable shape changes, you can use shape hints. Shape hints identify points that should correspond to starting and ending shapes. For example, if you are tweening the letter M into the letter N, you can use a shape hint to mark corner of the letter's shape. Then, instead of the letters becoming a jumble of lines while the shape change takes place, each letter remains recognizable and changes separately during the shift.
To use shape hints:
The SWF files animate the letter M into the letter N. The top SWF file does not use shape hints to morph the two letters, while the bottom SWF file uses shape hints to improve the morphing. Refer to the sample file shape_hints.fla, which is part of the animation_samples.zip archive that accompanies this article.
Shape hints contain letters (a through z) for identifying which points correspond in the starting and ending shape. You can use up to 26 shape hints. Shape hints are yellow in a starting keyframe, green in an ending keyframe, and red when within a fill area or outside the shape (Flash ignores red shape hints).
For best results when tweening shapes, follow these guidelines:
To create a frame-by-frame animation, you define each frame as a keyframe and create a different (typically modified) image for each frame. Each new keyframe on a layer typically contains the same contents as the keyframe preceding it because the contents of a frame are copied to the next keyframe when you select a frame and press F6. By selecting a frame and pressing F6, you can modify each new keyframe in the animation incrementally. The bee's wing moves only slightly so that a frame-by-frame animation was used to move the wing very slightly on sequential frames.
Often, you use the onion skin feature to view incremental changes between each keyframe. A motion tween has been applied to the lemur's arm and head. The onion skin tool enables you to view multiple frames of the animation at once.
To turn on onion skinning, click the Onion Skin or Onion Skin Outlines button near the bottom of the Timeline. Drag the markers above the Timeline to view multiple frames at once. The onion skin outlines are enabled for an animation on the Stage.
To create a frame-by-frame animation:
After you create a frame or a keyframe, you can move it elsewhere in the active layer or to another layer, remove it, and make other changes. Only keyframes are editable. You can view tweened frames, but you can't edit them directly. To edit tweened frames, you change one of the defining keyframes or insert a new keyframe between the beginning and ending keyframes. You can drag items from the Library panel onto the Stage to add the items to the current keyframe.
To display and edit more than one frame at a time, use onion skinning, covered next.
Inserting and Modifying Frames
To insert frames in the Timeline, do one of the following:
To simultaneously see several frames of an animation on the Stage, click the Onion Skin button. All frames between the Start Onion Skin and End Onion Skin markers (in the Timeline header) are superimposed as one frame in the Document window.
To control onion skinning display, do any of the following:
To change the display of onion skin markers, click the Modify Onion Markers button and select an item from the menu:
To move the entire animation to another location on the Stage:
Adding a Timeline Effect
When you add a Timeline effect to an object, Flash creates a layer and transfers the object to the new layer. The object is placed inside the effect graphic, and all tweens and transformations required for the effect reside in the graphic on the newly created layer. The new layer automatically receives the same name as the effect, appended with a number that represents the order in which the effect is applied, out of all effects in your document.
When you add a Timeline effect, a folder with the effect's name is added to the Library, containing elements used in creating the effect.
To add an effect to an object:
1 Do one of the following to add a Timeline effect:
3 In the dialog box that appears for the effect, view the effect preview based on default settings. Modify the default settings as desired, and then click Update Preview to view the effect with the new settings.
4 When the Timeline effect appears as desired in the preview window, click OK.
Editing a Timeline Effect
You edit Timeline effects using the Effect Settings dialog box:
1 Select the object associated with the effect on the Stage.
2 To open the Effect Settings dialog box, do one of the following:
Deleting a Timeline Effect
You use the context menu to delete Timeline effects. Right-click
(Windows) or Control-click (Macintosh) the object on the Stage that has the Timeline effect you want to remove, and select Timeline Effects > Remove Effect from the context menu.
About Scripting Animation
You can use ActionScript 2.0 to add animation to your Flash applications instead of using motion or shape tweens on a timeline. The following sections show you how to use code to animate instances, such as changing the transparency and appearance of the instance, and moving the instance around the Stage.
For information on using the Tween and TransitionManager classes to further automate code-based animations, see the following section, Using the Tween and TransitionManager Classes. These classes help you add advanced easing equations and transition animations to movie clip instances in your application. Many of these effects are difficult to recreate using ActionScript without these prebuilt classes because the code involves writing complex mathematical equations to achieve the effect.
The animation uses code to tween the bee horizontally across the Stage. Notice the easing that has been applied to the motion as well. This animation uses very few lines of code to achieve this effect.
Note: Issues regarding frame rate discussed in the earlier section, About Frame Rate and Animation, also apply to scripted animation.
Fading a Movie Clip
When you work with movie clips on the Stage, you might want to fade the movie clip in or out instead of toggling its _visible property. The following examples, taken from the Flash documentation, show you a variety of simple ways to use ActionScript to create animation.
The following procedure demonstrates how to use an onEnterFrame event handler to animate a movie clip. To fade a movie clip by using code:
This code uses an onEnterFrame event handler, which is invoked repeatedly at the frame rate of the SWF file. The number of times per second that the event handler is called depends on the frame rate at which the Flash document is set. If the frame rate is 12 fps, the onEnterFrame event handler is invoked 12 times per second. Likewise, if the Flash document's frame rate is 30 fps, the event handler is invoked 30 times per second.
img1_mc.onEnterFrame = function() {
img1_mc._alpha -= 5;
if (img1_mc._alpha <= 0) {
mg1_mc._visible = false;
delete img1_mc.onEnterFrame;
}
};
The setInterval() function behaves slightly differently than the onEnterFrame event handler because the setInterval() function tells Flash precisely how frequently the code should call a particular function. In this code example, the user-defined fadeImage() function is called every 50 milliseconds (20 times per second). The fadeImage() function decrements the value of the current movie clip's _alpha property. When the _alpha value is equal to or less than 0, the interval is cleared and the fadeImage() function stops executing.
var alpha_interval:Number = setInterval(fadeImage, 50, img1_mc);
function fadeImage(target_mc:MovieClip):Void {
target_mc._alpha -= 5;
if (target_mc._alpha <= 0) {
target_mc._visible = false;
clearInterval(alpha_interval);
}
}
This code example loads an external image from a remote web server and, when the image is fully loaded, animates it horizontally across the Stage. Instead of using an onEnterFrame event handler, you could use the setInterval() function to animate the image.
// Create a movie clip instance.
this.createEmptyMovieClip("img1_mc", 10);
var mcl_obj:Object = new Object();
mcl_obj.onLoadInit = function (target_mc:MovieClip):Void {
target_mc._x = Stage.width;
target_mc.onEnterFrame = function() {
target_mc._x -= 3; // decrease current _x position by 3 pixels
if (target_mc._x <= 0) {
target_mc._x = 0;
delete target_mc.onEnterFrame;
}
};
};
var img_mcl:MovieClipLoader = new MovieClipLoader();
img_mcl.addListener(mcl_obj);
// Load an image into the movie clip
img_mcl.loadClip("http://www.helpexamples.com/flash/images/image1.jpg",
img1_mc);
Using the Tween and TransitionManager Classes
When you install Flash Basic 8 or Flash Professional 8, you also install two powerful classes: the Tween and TransitionManager classes. This section describes how to use these classes with movie clips and Macromedia V2 components (included with Flash MX 2004 and Flash 8) to add animation easily to your SWF files.
If you create a slide presentation or form application with Flash Professional 8 (ActionScript 2.0 only), you can select behaviors that add different kinds of transitions between slides, which is similar to when you create a PowerPoint presentation. You add this functionality into a screen application by using the Tween and TransitionManager classes, which generate ActionScript that animates the screens depending on the behavior you choose.
You can also use the Tween and TransitionManager classes outside of a screen-based document in either Flash Basic 8 or Flash Professional 8. For example, you can use the classes with the component set of version 2 of the Macromedia Component Architecture, or with movie clips. If you want to change the way a ComboBox component animates, you can use the TransitionManager class to add some easing when the menu opens. You can also use the Tween and TransitionManager classes, instead of creating motion tweens on the Timeline or writing custom code, to create your own animated menu system.
Note: The Tween and TransitionManager classes are available only in ActionScript 2.0, but these classes are available in both Flash Basic 8 and Flash Professional 8.
Tween Class
The Tween class enables you to use ActionScript to move, resize, and fade movie clips easily on the Stage by specifying a property of the target movie clip to be tween-animated over a number of frames or seconds. The Tween class also enables you to specify a variety of easing methods. Easing refers to gradual acceleration or deceleration during an animation, which helps your animations appear more realistic. For example, the options on a drop-down list component you create might gradually increase their speed near the beginning of an animation as the options appear, but slow down before the options come to a full stop at the end of the animation as the list is extended. Flash provides many easing methods that contain equations for this acceleration and deceleration, which change the easing animation accordingly.
The Tween class also invokes event handlers so your code may respond when an animation starts, stops, or resumes or increments its tweened property value. For example, you can start a second tweened animation when the first tween invokes its Tween.onMotionStopped event handler, indicating that the first tween has stopped.
TransitionManager Class
The TransitionManager class and the effect-defining transition-based classes enable you to apply impressive transition animation effects quickly to slides and movie clips.
As its name implies, the TransitionManager class manages transitions by implementing animation events. It enables you to apply one of 10 animation effects to movie clips. The transition effects are defined in a set of transition classes that all extend the base class mx.transitions.Transition.
Combining Animation, Filters, and the Tween Class
You can use ActionScript, such as the Tween class, to animate filters at runtime, which enables you to apply interesting, animated effects to your Flash applications. In the following example, you see how to combine the Blur filter with the Tween class to create an animated blur that modifies the Blur filter between a value of 0 and 10 at runtime.
To animate blurs using the Tween class:
The preceding code is separated into three sections. The first section imports the required classes and packages. The second section creates a nested movie clip that is used to load an image and apply filters to the holder movie clip. The final section creates a new MovieClipLoader instance and a listener for the movie clip loader.
import flash.filters.BlurFilter;
import mx.transitions.Tween;
import mx.transitions.easing.*;
this.createEmptyMovieClip("holder_mc", 10);
holder_mc.createEmptyMovieClip("img_mc", 20);
var mclListener:Object = new Object();
mclListener.onLoadInit = function(target_mc:MovieClip) {
target_mc._x = (Stage.width - target_mc._width) / 2;
target_mc._y = (Stage.height - target_mc._height) / 2;
var myTween:Tween = new Tween(target_mc, "blur",
Strong.easeInOut, 0, 20, 3, true);
myTween.onMotionChanged = function() {
target_mc._parent.filters =
[new BlurFilter(target_mc.blur, target_mc.blur, 1)];
};
myTween.onMotionFinished = function() {
myTween.yoyo();
}
};
var my_mcl:MovieClipLoader = new MovieClipLoader();
my_mcl.addListener(mclListener);
my_mcl.loadClip("http://www.helpexamples.com/flash/images/image1.jpg",
holder_mc.img_mc);
The listener object defines a single event handler function, onLoadInit, which is started once the image successfully loads and is available on the Stage. First the image is repositioned to the center of the Stage and then a new Tween object is created that animates the movie clip and applies a Blur filter of 0 and 10.
The following procedure loads a JPEG image and dynamically masks the image so you can reveal the image slowly after it loads by tweening the image's mask.
To animate dynamic masks:
This code example imports the Tween class and each of the classes in the easing package. Next it creates an object that acts as the listener object for a MovieClipLoader instance that's created in a later section of the code. The listener object defines a single event listener, onLoadInit, which centers the dynamically loaded JPEG image on the Stage. After the code repositions the image, a new movie clip instance is created within the target_mc movie clip (which contains the dynamically loaded JPEG image). The Drawing API code draws a rectangle with the same dimensions as the JPEG image within this new movie clip. The new movie clip masks the JPEG image by calling the MovieClip.setMask() method. After the mask is drawn and set up, the mask uses the Tween class to animate, which causes the image to slowly reveal itself.
import mx.transitions.Tween;
import mx.transitions.easing.*;
var mclListener:Object = new Object();
mclListener.onLoadInit = function(target_mc:MovieClip) {
target_mc._visible = false;
// Center the image on the Stage.
target_mc._x = (Stage.width - target_mc._width) / 2;
target_mc._y = (Stage.height - target_mc._height) / 2;
var maskClip:MovieClip = target_mc.createEmptyMovieClip("mask_mc", 20);
with (maskClip) {
// Draw a mask that is the same size as the loaded image.
beginFill(0xFF00FF, 100);
moveTo(0, 0);
lineTo(target_mc._width, 0);
lineTo(target_mc._width, target_mc._height);
lineTo(0, target_mc._height);
lineTo(0, 0);
endFill();
}
target_mc.setMask(maskClip);
target_mc._visible = true;
var mask_tween:Object = new Tween
(maskClip, "_yscale", Strong.easeOut, 0, 100, 2, true); };
this.createEmptyMovieClip("img_mc", 10);
var img_mcl:MovieClipLoader = new MovieClipLoader();
img_mcl.addListener(mclListener);
img_mcl.loadClip("http://www.helpexamples.com/flash/images/image1.jpg",
img_mc);
You can use alpha masks if you apply runtime bitmap caching. See an sample FLA file demonstrate this feature on the Flash Samples page (http://macromedia.com/support/documentation/en/flash/fl8/samples.html#alpha_mask ).
Instructional Media Development (IMD)
Macromedia Documentation
Editor: Jen deHaan
www.flash8forums.com
Reviewer: Chris Georgenes
www.mudbubble.com