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