Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Animation, Filters, and Drawings > About the Tween and TransitionManager classes > Using the Tween class > Creating animations that run continuously | |||
You can make an animation continue moving back and forth along the x-axis without stopping. The Tween class accommodates this kind of animation with the aptly named yoyo() method. The yoyo() method waits for the onMotionFinished event handler to execute, and then it reverses the begin and finish parameters. The animation begins again, as the following procedure demonstrates.
To create an animation that continues endlessly:
import mx.transitions.Tween;
import mx.transitions.easing.*;
this.createEmptyMovieClip("box_mc", this.getNextHighestDepth());
with (box_mc) {
beginFill(0xFF0000, 60);
moveTo(0, 0);
lineTo(20, 0);
lineTo(20, Stage.height);
lineTo(0, Stage.height);
lineTo(0, 0);
endFill();
}
The first section code begins by importing the Tween class, as well as each class in the easing package. The next section of code creates a new movie clip with an instance name of box_mc and draws a rectangle 20 pixels wide and the same height as the Stage.
var box_tween:Tween = new Tween(box_mc, "_x", Regular.easeInOut, 0, Stage.width, 3, true);
box_tween.onMotionFinished = function() {
box_tween.yoyo();
};
This code creates a new tween to animate the box_mc movie clip across the Stage along the x-axis over 3 seconds.
The box animates from left to right and back. If the animation isn't smooth, you might want to increase the document's frame rate from 12 fps to 24 fps.
As the box approaches the right edge of the Stage, it animates outside the boundaries of the Stage. While this might not seem significant, you might not want the rectangle to disappear from view off the side of the Stage and then reappear a second later and animate in the other direction.
To make adjustments, animate the rectangle from 0 pixels to the width of the Stage minus the width of the box_mc movie clip.
var box_tween:Tween = new Tween(box_mc, "_x", Regular.easeInOut, 0, (Stage.width - box_mc._width), 3, true);
Now, the box stops easing before it goes off the boundaries of the Stage.
|
|
|
|