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 don’t 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()
).
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...