Tutorial > Use actions to streamline authoring

Use actions to streamline authoring

You're ready to add actions to your movie that determine which kite the customer selected and display the selected kite with the correct invoice. First you'll use the Frame Actions panel to create a function, a block of reusable code that performs a task. In this case, the task is to load a specific SWF movie of a kite into the Flash Player, depending on which kite the user selects.

Note: The SWFs of the different kite model and color combinations exist in your My_kite folder.

In addition to the function that you'll create, you'll also use the Include action to link to another function in an external text file.

Note: It's beyond the scope of this tutorial to teach ActionScript syntax; refer to ActionScript Help for additional information about creating ActionScript.


 
Create a function

You will name the function that you create refreshKite. If you think of your movie as the store that holds the kites, think of refreshKite as the salesperson who retrieves a kite for the customer.

A parameter, called currentKite, tells the refreshKite function which kite the customer selected: the kite model and color. A simple definition for a parameter, therefore, is that it's a placeholder that lets you pass information to a function.

1 In the Timeline, double-click the keyframe in Frame 1 of the actions layer.
The Frame Actions panel appears. If necessary, resize the window to view both panes. The Actions list already contains ActionScript, to which you will add new actions.
2 In the Toolbox list, click the Actions icon to expand it, then double-click function.
You can also drag the function icon to the bottom of the Actions list.
3 In the Name text box, type refreshKite.
4 In the Parameters text box, type currentKite.
The function will use the currentKite (the currently selected kite) parameter to identify the correct kite to display.
5 With the Actions category still expanded in the Toolbox list, double-click loadMovie.
You are telling Flash to replace the movie clip on the Stage with the SWF specified by the parameter.
6 In the URL text box, type currentKite+".swf"
7 Select Expression, to the right of the URL text box.
By selecting Expression, you are telling Flash that currentKite + ".swf" is not a literal string of characters, but rather a description. The function uses this description to determine the correct external file name.
8 In the Location pop-up menu, select Target, and in the text box to the right, type demoKite.
The symbol name for the placeholderKite instance, remember, is demoKite.
9 Verify that Don't Send is selected in the Variables pop-up menu.
10 In the Toolbox list, double-click set variable, which defines a new variable.
A variable is a container that holds information, such as which kite is selected. In your movie, the variable remembers the most recent kite selected.
11 In the Variable text box, type chosenKite, the name of the variable. Verify that Expression, to the right of the text box, is not selected.
12 In the Value text box, type currentKite. Select Expression, to the right of the text box.


 
Include an external function

You have learned that a function is a set of actions that perform tasks based on the information it receives from parameters. You will now include an external function in your ActionScript that creates an invoice based on the selected kite. The external function, named kiteFunction.txt, is in a text file in the Tutorial/My_kite folder within your Flash 5 application folder. To link to the external file, you use the Include action.

One benefit of linking to an external function rather than making the function a part of your movie is that if the function changes, you do not have to update your movie.

The external function demonstrates how concise, yet powerful, ActionScript can be:

function generateInvoice (Style, Color, Price, currentKite) {

_root.invoice.invoiceStyle = Style;
_root.invoice.invoiceColor = Color;
_root.invoice.invoicePrice = Price;
flyingKite = currentKite;
}

Notice, however, that the function is not commented. It's a good idea to add comments to your ActionScript, which is like adding notes about the purpose of the script, which might otherwise take some effort to understand. Here's the same function with explanatory comments. ActionScript comments appear after double slashes (//), which indicate to Flash that it should ignore the text after the slashes on that line.

function generateInvoice (Style, Color, Price, currentKite) {
//Sets the invoiceStyle variable of the invoice movie clip to the //value of the Style parameter

_root.invoice.invoiceStyle = Style;
//Sets the invoiceColor variable of the invoice movie clip to the //value of the Color parameter
_root.invoice.invoiceColor = Color;
//Sets the invoicePrice variable of the invoice movie clip to the //value of the Price parameter
_root.invoice.invoicePrice = Price;
//Sets the variable flyingKite equal to the variable currentKite
flyingKite = currentKite;
}

Now you will add the include script that links the internal function that you created to the external function.

1 In the Frame Actions panel Toolbox list, under the Actions category, drag the include icon to the end of the text in the Actions list.
2 In the Path text box, type KiteFunction.txt.
3 Close the Frame Actions panel.

Note: Remember to save your work frequently.