EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 208

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
208 Functions and Methods
6. Select Control > Test Movie to test the SWF file.
You use the
function statement to create your own function in ActionScript. Remember that
parameters are optional; however, if you dont have parameters, you still need to include the
brackets. The content between the curly braces (
{}) is called the function block.
You can write functions on the main timeline or within external ActionScript files, including
class files.
You also write constructor functions in class files using this format (however, the name of the
function matches the class). For more information on constructor functions, see Writing the
constructor function” on page 268. Also see Chapter 7, “Classes,” on page 225 for
information on and examples of writing functions in classes.
Writing anonymous and callback functions
A named function is a function that you reference in your script before or after you define it,
whereas an anonymous function is an unnamed function that references itself; you reference the
anonymous function when you create it. When you write ActionScript code, you will create
many anonymous functions.
Anonymous functions are commonly used when you work with event handlers. To write an
anonymous function, you could store a function literal inside a variable. Therefore, you can
reference the function later in your code. The next example shows you how to write an
anonymous function.
To write an anonymous function:
1. Create a movie clip on the Stage, and then select the clip.
2. Open the Property inspector, and type my_mc into the Instance Name text box.
3. Select Frame 1 of the Timeline, and type the following code into the Actions panel:
var myWidth = function () {
trace(my_mc._width);
};
//later in code you can add
myWidth();
4.
Select Control > Test Movie.
The width of the movie clip is displayed in the Output panel.
You can also create a function inside an object, such as an XML or LoadVars instance. You can
associate an anonymous function with a certain event to create a callback function. A function
calls a callback function after a specific event occurs, such as after something finishes loading
(
onLoad()) or finishes animating (onMotionFinished()).

Table of Contents

Related product manuals