Using built-in functions
A function is a block of ActionScript code that can be reused anywhere in a SWF file. If you pass
values as parameters to a function, the function will operate on those values. A function can also
return values.
Flash has built-in functions that let you access certain information and perform certain tasks, such
as getting the version number of Flash Player hosting the SWF file (
that belong to an object are called methods. Functions that don't belong to an object are called top-
level functions and are found in the Functions category of the Actions panel.
Each function has its own characteristics, and some functions require you to pass certain values. If
you pass more parameters than the function requires, the extra values are ignored. If you don't
pass a required parameter, the empty parameters are assigned the
cause errors when you export a script. To call a function, it must be in a frame that the playhead
has reached.
To call a function, simply use the function name and pass any required parameters:
isNaN(someVar);
getTimer();
eval("someVar");
For more information on each function, see its entry in
on page
205.
Creating functions
You can define functions to execute a series of statements on passed values. Your functions can
also return values. After a function is defined, it can be called from any Timeline, including the
Timeline of a loaded SWF file.
A well-written function can be thought of as a "black box." If it has carefully placed comments
about its input, output, and purpose, a user of the function does not need to understand exactly
how the function works internally.
Defining a function
Functions, like variables, are attached to the Timeline of the movie clip that defines them, and
you must use a target path to call them. As with variables, you can use the
declare a global function that is available to all Timelines without using a target path. To define a
global function, precede the function name with the identifier
following example:
_global.myFunction = function (x) {
return (x*2)+3;
}
To define a Timeline function, use the
any parameters to be passed to the function, and the ActionScript statements that indicate what
the function does.
The following example is a function named
function areaOfCircle(radius) {
return Math.PI * radius * radius;
}
Chapter 12, "ActionScript Dictionary,"
action followed by the name of the function,
function
with the parameter
areaOfCircle
). Functions
getVersion()
data type, which can
undefined
identifier to
_global
, as shown in the
_global
radius
Creating functions
:
51
Need help?
Do you have a question about the FLASH MX 2004 - ACTIONSCRIPT and is the answer not in the manual?
Questions and answers