EasyManua.ls Logo

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

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...
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 dont 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));
TIP
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).

Table of Contents

Related product manuals