Flash 8 Documentation |
|||
| Learning ActionScript 2.0 in Flash > Animation, Filters, and Drawings > About the Tween and TransitionManager classes > Using the Tween class | |||
If you use the Tween class in more than one place in your Flash document, you might opt to use an import statement. This lets you import the Tween class and easing methods rather than give the fully qualified class names each time you use them, as the following procedure shows.
import mx.transitions.Tween; import mx.transitions.easing.*; new Tween(ball_mc, "_x", Elastic.easeOut, Stage.width, 0, 3, true);
This code example uses two import statements. The first statement imports the mx.transitions.Tween class only, and the second import statement uses the wildcard (*) shortcut to import each of the six easing classes by using a single line of code. The second statement imports an entire package of classes.
|
NOTE |
|
For information on working with packages, see Working with filter packages. |
Flash documentation defines package as directories that contain one or more class files and that reside in a designated classpath directory. In this case, the package resides in the C:\Program Files\Macromedia\Flash 8\language\First Run\Classes\mx\transitions\easing folder (Windows), or HD:Applications:Macromedia Flash 8:First Run:Classes:mx:transitions:easing (Macintosh). You might agree that importing an entire package is much better than having to import the six classes separately. Instead of referring to the mx.transitions.Tween class, your ActionScript directly refers to the Tween class. Likewise, instead of using the fully qualified class name for the easing classes, mx.transitions.easing.Elastic.easeOut for example, you can type Elastic.easeOut in your ActionScript code. For more information, see Working with filter packages.
Using similar code, you set the _alpha property to fade instances in and out, instead of the _x property, as the next procedure shows.
import mx.transitions.Tween; import mx.transitions.easing.*; new Tween(ball_mc, "_alpha", Strong.easeIn, 100, 0, 3, true);
Instead of moving around the Stage, now ball_mc fades from 100% visible to completely transparent in three seconds. To make the symbol fade out more quickly, change the duration parameter from 3 to 1 or 2.
If you change the document's frame rate, the animation appears to play more smoothly. For information on animation and frame rate, see About animation and frame rate.
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 in seconds. If you set the parameter to false, the duration is the number of frames you want to use for the tween. The next procedure shows how to set a tween to frames instead of seconds.
import mx.transitions.Tween; import mx.transitions.easing.*; new Tween(ball_mc, "_alpha", Strong.easeIn, 100, 0, 24, false);
This code 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.
Wait a moment, then the instance fades out across 24 frames.
If you increase the frame rate of your FLA file, you see the instance fade out sooner. For information on animation and frame rate, see About animation and frame rate.
Using frames instead of seconds offers more flexibility, but remember that the duration relates to the frame rate of the current Flash document. If your Flash document uses a frame rate of 12 frames per second (fps), the previous code snippet fades the instance over two seconds (24 frames/12 fps = 2 seconds). However, if your frame rate is 24 fps, 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.
The Tween class has several more useful features. For example, you can write an event handler that triggers when the animation completes, as the next procedure shows.
import mx.transitions.Tween;
import mx.transitions.easing.*;
var tween_handler:Object = new Tween(ball_mc, "_alpha", Strong.easeIn, 100, 0, 3, true);
tween_handler.onMotionFinished = function() {
trace("onMotionFinished triggered");
};
If you test this ActionScript in your FLA file, you see the message "onMotionFinished triggered" appear in the Output panel after ball_mc finishes fading on the Stage.
Wait for a moment, and then the instance fades out. When it finishes tweening, you see the message appear in the Output panel.
For more information on functions, see Classes.
Version 8
RSS feed | Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/8/main/00001502.html
Comments
helentriolo said on Oct 30, 2005 at 9:59 AM :