Tutorials
Flash AS3 Tween Events
In this tutorial, we'll take a look at how to work with events related to the Flash AS3 Tween class. We'll first take a look at the AS3 tween event known as
MOTION_FINISH. This event allows us to write some code that will respond when the tween animation is finished. Let's say you wanted a song to play, or an image to load when the tween finishes, then you can use this event to make that happen.
Step 1Use the oval tool to draw a circle, and convert it into a movie clip symbol. Then place two instances of this symbol on the stage. Name them
circle1_mc and
circle2_mc.
Step 2Create a new layer for the ActionScript. Select the first frame of this layer, and then open up the Actions panel.
Step 3Add the following code:
import fl.transitions.Tween;
import fl.transitions.easing.*;
circle1_mc.y = 100;
circle2_mc.y = 250;
var c1TweenX:Tween = new Tween(circle1_mc, "x", Bounce.easeOut, 100, 400, 3, true);
var c2TweenX:Tween = new Tween(circle2_mc, "x", Bounce.easeOut, 100, 400, 6, true);
The first 2 lines import the necessary classes that will make our tween work.
The 2 lines after that will position the circles so that
circle1_mc is above
circle2_mc.
The last 2 lines create the tween objects that will animate each circle. The first one is named
c1TweenX and animates
circle1_mc, the second one is named
c2TweenX and animates
circle2_mc. They both create a similar animation, but
circle1_mc has a duration of 3 seconds, while
circle2_mc has a duration of 6 seconds.
Test the movie to preview the animation. You should see both circles move from the left to the right.
Step 4In this next step, we'll import the
TweenEvent class so that we can create
TweenEvent handlers. Add the line in bold found below:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
circle1_mc.y = 100;
circle2_mc.y = 250;
var c1TweenX:Tween = new Tween(circle1_mc, "x", Bounce.easeOut, 100, 400, 3, true);
var c2TweenX:Tween = new Tween(circle2_mc, "x", Bounce.easeOut, 100, 400, 6, true);
Step 5Now that we've imported the
TweenEvent class, let's create an event listener function that will get called every time one of our tweens finishes. Let's name this function
onMotionFinish. When this function gets called, let's tell Flash to trace the phrase
"motion is finished".
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
circle1_mc.y = 100;
circle2_mc.y = 250;
var c1TweenX:Tween = new Tween(circle1_mc, "x", Bounce.easeOut, 100, 400, 3, true);
var c2TweenX:Tween = new Tween(circle2_mc, "x", Bounce.easeOut, 100, 400, 6, true);
function onMotionFinish(e:TweenEvent):void
{
trace("motion is finished");
}
Step 6Next, we need to register the event listener function to the tween objects so that the function will get called when a tween finishes. The event we want to specify is
TweenEvent.MOTION_FINISH. This event gets announced whenever a tween finishes. When creating TweenEvent handlers, the event listener functions are added to the tween objects. So lets use the
addEventListener() method to register the
onMotionFinish event listener function to both of our tween objects (
c1TweenX and
c2TweenX).
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
circle1_mc.y = 100;
circle2_mc.y = 250;
var c1TweenX:Tween = new Tween(circle1_mc, "x", Bounce.easeOut, 100, 400, 3, true);
var c2TweenX:Tween = new Tween(circle2_mc, "x", Bounce.easeOut, 100, 400, 6, true);
function onMotionFinish(e:TweenEvent):void
{
trace("motion is finished");
}
c1TweenX.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
c2TweenX.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
Step 7Next, let's modify the trace statement in the event listener function so that it will also indicate which movie clip has just finished tweening. In the trace statement, add
e.target.obj.name e.target refers to the tween object (either
c1TweenX or
c2TweenX). Adding
obj.name identifies the name of the object being tweened (either
circle1_mc or
circle2_mc).
Look at the code below to see the updated trace statement:
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
circle1_mc.y = 100;
circle2_mc.y = 250;
var c1TweenX:Tween = new Tween(circle1_mc, "x", Bounce.easeOut, 100, 400, 3, true);
var c2TweenX:Tween = new Tween(circle2_mc, "x", Bounce.easeOut, 100, 400, 6, true);
function onMotionFinish(e:TweenEvent):void
{
trace(e.target.obj.name + " " + "motion is finished");
}
c1TweenX.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
c2TweenX.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
Step 8Now, let's test the movie. Pay attention to the moving circles and the output window. You'll see the message in our trace statement come out every time one of the tweens finishes. Since
c1TweenX has a shorter duration, you'll see the message
"circle1_mc motion is finished" come out first, followed by
"circle2_mc motion is finished".
Step 9In this next step, we'll make the tweens start again each time it finishes.
In the event listener function, add
e.target.start(); right after the trace statement.
The
start() method of the Tween class will make the tween start from the beginning again. So since we are putting the call to the
start() method inside the
MOTION_FINISH event listener function, our tweens will start again each time they finish.
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
circle1_mc.y = 100;
circle2_mc.y = 250;
var c1TweenX:Tween = new Tween(circle1_mc, "x", Bounce.easeOut, 100, 400, 3, true);
var c2TweenX:Tween = new Tween(circle2_mc, "x", Bounce.easeOut, 100, 400, 6, true);
function onMotionFinish(e:TweenEvent):void
{
trace(e.target.obj.name + " " + "motion is finished");
e.target.start();
}
c1TweenX.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
c2TweenX.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
So now, our tweens will just keep repeating.
Step 10In this next step, let's change the
start() method to the
yoyo() method. The
yoyo() method of the Tween class creates a looping effect as well. The difference is that it will alternately play the animation in reverse, just like how a yoyo toy goes down then up then down then up, and so on... So in the case of our animation, you'll see the circles yoyo back and forth from left to right and right to left.
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;
circle1_mc.y = 100;
circle2_mc.y = 250;
var c1TweenX:Tween = new Tween(circle1_mc, "x", Bounce.easeOut, 100, 400, 3, true);
var c2TweenX:Tween = new Tween(circle2_mc, "x", Bounce.easeOut, 100, 400, 6, true);
function onMotionFinish(e:TweenEvent):void
{
trace(e.target.obj.name + " " + "motion is finished");
e.target.yoyo();
}
c1TweenX.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
c2TweenX.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinish);
So here, you've seen a demonstration of how a
MOTION_FINISH event handler for Tween objects work. You can use it to tell Flash to do something whenever a tween object finishes an animation.
There are a few more events in the TweenEvent class. I've listed them below with short descriptions included.
List of events in the TweenEvent ClassMOTION_CHANGE As the tween animates, this event will constantly get dispatched. It gets triggered each time the position, size, rotation, or alpha level of the object being tweened changes.
MOTION_FINISHDispatched when a tween finishes.
MOTION_LOOPDispatched when the tween starts playing again when in looping mode. The loop property of the Tween object must be set to true in order for this event to get dispatched.
MOTION_RESUMEDispatched when a call to the
resume() method of the Tween class has been made.
MOTION_STARTDispatched when a call to the
start() method of the Tween class has been made.
MOTION_STOPDispatched when a call to the
stop() method of the Tween class has been made.
And that concludes this tutorial on Flash AS3 Tween events.
-
The Flash As3 Tween Class
In this tutorial, we're going to learn how to use the Flash AS3 Tween Class. The tween class lets you create tween animation using code. Step 1 Let's first create a movie clip that we will use with our ActionScript 3 tween. Draw a small circle...
-
Flash 8 Masking Effect | How To Create A Mask In Flash 8 Video Tutorials
In this series of Macromedia Flash 8 video tutorial clips, we'll show you how to create a Flash 8 Masking effect. A Flash 8 mask layer can be used to hide some portions of a layer underneath it, revealing only select areas. It's pretty simple...
-
Flash 8 Motion Tween Video Tutorials | The Basics Of Motion Tweening - Animating Position, Color, Size And Using The Easing Property
In this set of Macromedia Flash tutorial online video training clips, we'll show you the basics of Motion Tweens in Macromedia Flash 8. In these tutorials, you'll learn how to animate the following properties of an object: position - make an object...
-
Flash 8 Motion Guide Animation Tutorial Video Series
This tutorial was made for Flash 8. For Flash CS5 users, check out adding a motion guide in Flash CS5. In this Macromedia Flash 8 tutorial video training series, we'll teach you how to use the Flash 8 Motion Guide. When creating motion tween movements...
-
Loading External Text Files In Flash Actionscript 3.0
Work Files: Load_Text_Start.fla summer.txt (right-click > save as ) In this article, we're going to learn how to load text from an external source into a Flash movie. What is the benefit of loading text from an external source? The nice thing...
Tutorials