MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH Manual page 215

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

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.
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).
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
you test your SWF file at this time, nothing happens. For example, you don't see the
statement in the Output panel. To see the
trace
function.
helloWorld()
4.
Type the following line of ActionScript code after the function:
helloWorld();
This code calls the
5.
Select Control > Test Movie to test the FLA file.
The following text is displayed in the Output panel:
For information on passing values (parameters) to a function, see
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));
function.
helloWorld()
helloWorld()
statement, you have to call the
trace
Hello world!
"Passing parameters to a
About functions and methods
. If
215

Hide quick links:

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flash 8

Table of Contents