EasyManua.ls Logo

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT - Using Built-In Functions; Creating Functions

MACROMEDIA FLASH MX 2004 - ACTIONSCRIPT
816 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...
Creating functions 51
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 (
getVersion()). Functions
that belong to an object are called methods. Functions that dont 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 dont
pass a required parameter, the empty parameters are assigned the
undefined data type, which can
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 Chapter 12, “ActionScript Dictionary,
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
_global identifier to
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
_global, as shown in the
following example:
_global.myFunction = function (x) {
return (x*2)+3;
}
To define a Timeline function, use the function action followed by the name of the function,
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
areaOfCircle with the parameter radius:
function areaOfCircle(radius) {
return Math.PI * radius * radius;
}

Table of Contents

Related product manuals