Flash CS3 Documentation |
|||
| Learning ActionScript 2.0 in Adobe Flash > Functions and Methods > About functions and methods > About types of methods and functions > About built-in and top-level functions | |||
As discussed in About functions and methods, 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 operates on those values. A function can also return values.
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; 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.
|
NOTE |
|
To call a function, that function's definition must be in a frame that the playhead has reached. |
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. For information on each function, see its entry in ActionScript 2.0 Language Reference.
Flash CS3
Send me an e-mail when comments are added to this page | Comment Report
Current page: http://livedocs.adobe.com/flash/9.0/main/00000749.html