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.
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...