Writing Scripts with ActionScript > Using predefined functions

Using predefined functions

A function is a block of ActionScript code that can be reused anywhere in a movie. If you pass specific values called arguments to a function, the function will operate on those values. A function can also return values. Flash has predefined functions that allow you to access certain information and perform certain tasks, such as collision detection (hitTest), getting the value of the last key pressed (keycode), and getting the version number of the Flash Player hosting the movie (getVersion).


 
Calling a function

You can call a function in any Timeline from any Timeline, including a loaded movie. Each function has its own characteristics and some require you to pass certain values. If you pass more arguments than the function requires, the extra values are ignored. If you don't pass a required argument, the empty arguments 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.

Flash's predefined functions are listed in the following table:

Boolean

getTimer

isFinite

newline

scroll

escape

getVersion

isNaN

number

String

eval

globalToLocal

keycode

parseFloat

targetPath

false

hitTest

localToGlobal

parseInt

true

getProperty

int

maxscroll

random

unescape


Note: String functions are deprecated and are not listed in the above table.

To call a function in Expert Mode:

Use the name of the function. Pass any required arguments inside parentheses.

The following example calls the initialize function which requires no arguments:

initialize();

To call a function in Normal Mode:

Use the evaluate action. Enter the function name and any required arguments in the Expression field.

Use the evaluate action to call a function in Normal Mode
 

To call a function on another Timeline use a target path. For example, to call the function calculateTax that was declared in the instance functionsMovieClip, use the following path:

_root.functionsMovieClip.calculateTax(total);

Note: Pass any arguments inside the parentheses.

For more information on each function, including deprecated string functions, see individual entries in ActionScript dictionary: Overview.