206
Functions and Methods
You can use functions that are built into the ActionScript language. They might be top level,
as described in
“About types of methods and functions” on page 203
; or the function might
be in a built-in class, such as Math or MovieClip, which you use as a method in your
application.
You use built-in functions in ActionScript to perform certain tasks and to access information.
For example, you can get the number of milliseconds the SWF file has been playing by using
getTimer()
. Or you can get the version number of Flash Player that hosts the file by using
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 subcategories of the Global
Functions category of the Actions panel.
Some built-in 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 during
runtime.
Top-level functions are easy to use. To call a function, simply use the function name and pass
any parameters required by that function. (For information on required parameters, see the
entry for the function in the
ActionScript 2.0 Language Reference
). For example, add the
following ActionScript to Frame 1 of the Timeline:
trace("my message");
When you test the SWF file,
my message
appears in the Output panel. Two other examples
of top-level functions are
setInterval()
and
getTimer()
. The next example shows how to
use both of these functions together. Add the following code to Frame 1 of the Timeline:
function myTimer():Void {
trace(getTimer());
}
var intervalID:Number = setInterval(myTimer, 100);
This code creates a simple timer using
getTimer()
, and uses the
setInterval()
and
trace()
top-level functions to display the number of milliseconds since the SWF file began
to play in Flash Player.
Calling a top-level function is like calling a
user-defined function
. For more information, see
“Targeting and calling user-defined functions” on page 213
. For information on each
function, see its entry in
ActionScript 2.0 Language Reference
.
NO
TE
To call a function, that function’s definition must be in a frame that the playhead has
reached.
Summary of Contents for FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Page 1: ...Learning ActionScript 2 0 in Flash...
Page 8: ...8 Contents...
Page 18: ...18 Introduction...
Page 30: ...30 What s New in Flash 8 ActionScript...
Page 66: ...66 Writing and Editing ActionScript 2 0...
Page 328: ...328 Interfaces...
Page 350: ...350 Handling Events...
Page 590: ...590 Creating Interaction with ActionScript...
Page 710: ...710 Understanding Security...
Page 730: ...730 Debugging Applications...
Page 780: ...780 Deprecated Flash 4 operators...
Page 830: ...830 Index...