About functions and methods
215
You can create functions in a FLA file or a class file or write ActionScript code that resides in
a code-based component. The following examples show you how to create functions on a
timeline and in a class file.
The following example shows you how to create and call a function in a FLA file.
To create and call a function in a FLA file:
1.
Create a new Flash document and save it as
basicFunction.fla
.
2.
Select Window > Actions to open the Actions panel.
3.
Type the following ActionScript code into the Script pane:
function helloWorld(){
// statements here
trace("Hello world!");
};
This ActionScript defines the (user-defined, named) function called
helloWorld()
. If
you test your SWF file at this time, nothing happens. For example, you don’t see the
trace
statement in the Output panel. To see the
trace
statement, you have to call the
helloWorld()
function.
4.
Type the following line of ActionScript code after the function:
helloWorld();
This code calls the
helloWorld()
function.
5.
Select Control > Test Movie to test the FLA file.
The following text is displayed in the Output panel:
Hello world!
For information on passing values (parameters) to a function, see
“Passing parameters to a
function” on page 218
.
There are several different ways that you can write functions on the main timeline. Most
notably, you can use named functions and anonymous functions. For example, you can use
the following syntax when you create functions:
function myCircle(radius:Number):Number {
return (Math.PI * radius * radius);
}
trace(myCircle(5));
TI
P
By packing your code into class files or code-based components, you can easily share,
distribute, or reuse blocks of code. Users can install your component, drag it onto the
Stage, and use the code that you store in the file, such as the workflow for code-based
components available in Flash (Window > Common Libraries > Classes).
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...