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:
is the unique name of the function. All function names in a document
functionName
must be unique.
contains one or more parameters that you pass to the function. Parameters
parameters
are sometimes called arguments. For more information on parameters, see
parameters to a function" on page
// function block
function. This part contains the statements that "do stuff." You can put the code that you
want to execute here. The
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
script to call a
trace()
218.
contains all of the ActionScript code that's carried out by the
// function block
function.
comment is a placeholder for where your
, which you use later in the
myMessage
About functions and methods
"Passing
207
Need help?
Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?