EasyManua.ls Logo

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

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 207
Writing named functions
A named function is a kind of function that you commonly create in your ActionScript code
to carry out all kinds of actions. When you create a SWF file, the named functions are
compiled first, which means that you can reference the function anywhere in your code, as
long as the function has been defined in the current or a previous frame. For example, if a
function is defined in Frame 2 of a timeline, you cannot access that function in Frame 1 of
the timeline.
The standard format for named functions is as follows:
function functionName(parameters) {
// function block
}
This piece of code contains the following parts:
functionName is the unique name of the function. All function names in a document
must be unique.
parameters contains one or more parameters that you pass to the function. Parameters
are sometimes called arguments. For more information on parameters, see “Passing
parameters to a function” on page 218.
// function block contains all of the ActionScript code that’s carried out by the
function. This part contains the statements that “do stuff.” You can put the code that you
want to execute here. The
// function block comment is a placeholder for where your
code for the function block would go.
To use a named function:
1. Create a new document called namedFunc.fla.
2. Import a short sound file into the library by selecting File > Import > Import to Library
and selecting a sound file.
3. Right-click the sound file and select Linkage.
4. Type mySoundID in the Identifier text box.
5. Select Frame 1 of the Timeline and add the following code to the Actions panel:
function myMessage() {
trace("mySoundID completed");
}
var my_sound:Sound = new Sound();
my_sound.attachSound("mySoundID");
my_sound.onSoundComplete = myMessage;
my_sound.start();
In this code you create a named function called myMessage, which you use later in the
script to call a
trace() function.

Table of Contents

Related product manuals