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 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
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;
}
Summary of Contents for FLASH MX 2004 - ACTIONSCRIPT
Page 1: ...ActionScript Reference Guide...
Page 8: ...8 Contents...
Page 12: ......
Page 24: ...24 Chapter 1 What s New in Flash MX 2004 ActionScript...
Page 54: ...54 Chapter 2 ActionScript Basics...
Page 80: ...80 Chapter 3 Writing and Debugging Scripts...
Page 82: ......
Page 110: ...110 Chapter 5 Creating Interaction with ActionScript...
Page 112: ......
Page 120: ...120 Chapter 6 Using the Built In Classes...
Page 176: ......
Page 192: ...192 Chapter 10 Working with External Data...
Page 202: ...202 Chapter 11 Working with External Media...
Page 204: ......
Page 782: ...782 Chapter 12 ActionScript Dictionary...
Page 793: ...Other keys 793 221 222 Key Key code...
Page 794: ...794 Appendix C Keyboard Keys and Key Code Values...
Page 798: ...798 Appendix D Writing Scripts for Earlier Versions of Flash Player...
Page 806: ...806 Appendix E Object Oriented Programming with ActionScript 1...
Page 816: ...816 Index...