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 DialogCongratulations 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 |
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 |
6 |
Type Scramble in the Method text box. |
The |
|
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 |
|
With the |
|
7 |
Double-click the |
8 |
From the Actions > Miscellaneous Actions category of the Actions toolbox, double-click the |
9 |
From the Properties category in the Actions toolbox, double-click |
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. |
![]() ![]() ![]() |