Introduction to ActionScript Tutorial > Create commands and reuse code > Call a function

 

Call a function

Functions can be called from any frame in a movie where you need a task completed. You must use a target path to call a function, just as you must use a path to access a variable or a movie clip. All functions in the puzzle.fla movie are declared in the first frame of the Actions layer, in the main Timeline, so the absolute path to each begins with _root.

Now you'll call the function that scrambles the puzzle pieces on the Stage.

1

In the Timeline, hide the Scramble dialog and Start dialog layers, if they're not already hidden. Select the Congrats dialog layer.

2

On the Stage, double-click the Dialog—Congratulations symbol.

3

On the Stage, select the OK button. If the Actions panel isn't open, choose Window > Actions.

4

In the Actions > User-Defined Functions category of the Actions toolbox, double-click the call function action.

5

With the insertion point in the Object text box of the Actions panel, click the Insert Target Path button. Verify Dots and Absolute are selected. Select _root and click OK.

6

Type Scramble in the Method text box.

The Scramble function doesn't require any parameters; you can leave the Parameters text box empty.

The code now looks like this:

on (release) {
    _root.Scramble();
}

This is the ActionScript that calls the function. You'll also need to add a few additional lines of code that belong inside the on(release) handler. You'll do that in the next steps.

With the _root.Scramble... line of code selected in the Script pane, double-click evaluate from the Actions > Miscellaneous Actions category in the Actions toolbox. Type _root.scramblebutton._visible = true in the Expression text box.

7

Double-click the evaluate action again, to add another blank line. Type _root.dialog = false in the Expression text box.

8

From the Actions > Miscellaneous Actions category of the Actions toolbox, double-click the evaluate action.

9

From the Properties category in the Actions toolbox, double-click _visible. With the insertion point after _visible, type = 0 in the Expression text box.

This code specifies that the dialog box is not visible after the user clicks the OK button.

The final code appears as follows:

on (release) {
	_root.Scramble();
	_root.scramblebutton._visible = true;
	_root.dialog = false;
	_visible = 0
}

10

Do one of the following to return to the main Timeline:

Choose Edit > Edit Document.

Click the Back button.

Click Scene 1.

11

Choose File > Save As and enter a new filename. Use a consecutive naming scheme so you can revert to earlier versions of the file, if necessary.

Note: As you complete the tutorial, remember to save your work frequently.