Writing an ActionScript 3 Tween Function

Writing an ActionScript 3 Tween Function
Now that we have set the foundation for the Tween animation in our FlashR project, we are ready to code the ActionScript that will run the animation. First, we will add an Event Listener to listen for a mouse click. When a mouse click occurs, a function will run that will create the Tween. Open the project that you have been working on with the previous tutorial and let's get started.

First, we need to decide what will start the Tween animation. Will it start as soon as the Flash file loads into the browser or will it be started by a certain event. Let's use the second method by adding an Event Listener to the stage.

  1. This Event Listener will call the showRec function when a mouse click occurs.

    stage.addEventListener(MouseEvent.CLICK, showRec);

  2. Next, we will write the code for the showRec function. Let's start the function code with the usual function keyword and brackets. As you can see, we add the name of the function after the function keyword. Also, we tie the function to the MouseEvent Event Listener.

    function showRec(event:MouseEvent):void
    { }

  3. Now we are ready to add our first line of the function code between the brackets. We will make a variable that will create an instance of the recMC movie clip, which is stored in the Library.

    var newRec:recMC = new recMC();

  4. With the next line of code, we add this new instance of the movie clip to the display list. This will make the rectangle visible on the stage.


    Finally, we are ready to add the Tween code. We will create the variables that we will use to Tween our object, the rectangle. To move an object on the stage, we need at least one variable that will control one property of the object. For example, if we want to move our rectangle horizontally across the stage, we need to use a variable to control the x location of the object. On the other hand, to move the rectangle both horizontally and vertically, we will need a variable for both the x and y locations.

  5. In the sample code below, we have done just that. Our new variables are xMove and yMove. The Tween code after the colon tells Flash that these are variables of the Tween type.

    var xMove:Tween = new Tween(newRec, "x", Back.easeIn, 100, 150, 2, true);
    var yMove:Tween = new Tween(newRec, "y", Back.easeIn, 100, 150, 2, true);

Let's look at what comes after the equal sign. This code initializes our new xMove and yMove variables with the Tween class constructors. As you can see, there are several arguments between the parenthesis. Let's take a look at each.

var xMove:Tween = new Tween(newRec, "x", Back.easeIn, 100, 150, 2, true);

This argument identifies the object that we will animate.

This argument identifies the object's property that we want to control, the x location of the object.

This argument identifies the type of easing that we want to use. There are several easing types, or classes, that come with the fl.transitions.easing package. They are Back, Bounce, Elastic, None, Regular and Strong. Here we are using the Back class.

Notice that the Back class is followed by a period and then the easeIn method. There are three easing functions to choose from including easeIn, easeInOut and easeOut.

This next argument will give Flash the initial x location for the object on the stage. This is the spot where the animation will begin. Because, our rectangle is not on the stage until we click the mouse, we are also telling Flash where to draw the rectangle on the stage.

This argument gives Flash the end location for the Tween. We will be moving the rectangle 50 pixels on the stage. So we need to set this argument to 150. Another option would be to move the rectangle to the location on the stage where we clicked the mouse.

This argument tells Flash the duration of the Tween animation. This number value can refer to either seconds or frames, depending on the value of the last argument.

This argument tells Flash what measurement to use for the Tween. When this argument is set to true, Flash will measure in seconds. For a false value, Flash will measure the duration in frames.

The second statement does the same for the y property of the object. Here is the code for our sample Tween.

var yMove:Tween = new Tween(newRec, "y", Back.easeIn, 100, 150, 2, true);

← Back

Copyright 2018 Adobe Systems Incorporated. All rights reserved. Adobe product screen shot(s) reprinted with permission from Adobe Systems Incorporated. Adobe, Photoshop, Photoshop Album, Photoshop Elements, Illustrator, InDesign, GoLive, Acrobat, Cue, Premiere Pro, Premiere Elements, Bridge, After Effects, InCopy, Dreamweaver, Flash, ActionScript, Fireworks, Contribute, Captivate, Flash Catalyst and Flash Paper is/are either [a] registered trademark[s] or a trademark[s] of Adobe Systems Incorporated in the United States and/or other countries.

Related Articles
Editor's Picks Articles
Top Ten Articles
Previous Features
Site Map

Content copyright © 2019 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact Diane Cipollo for details.