• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint

Manipulating Variables

You can use ActionScript to manipulate the information contained in variables. You might take the information from an input text box, perform some mathematical operation on it, and then display the result in a dynamic text box. The variable names are what allow you to tell Flash where to get existing information and where to display new information. You do this within an ActionScript by using the set variable action. The process of telling Flash what information to put in a variable is called assigning the variable.

To create a button script that manipulates variables:

Continuing with the file you created in the preceding exercises, on the Stage, in Keyframe 1, place an instance of a button.

With the arrow tool, select the button.

Access the Actions-Button panel.

In the Actions Toolbox, in the Actions > Variable category, double-click set variable (Figure 15.9).

Figure 15.9. The first step in creating a button that manipulates the text your users input is directing Flash to the variable that holds that text. Choose set variable from the Actions > Variables category.

Flash updates the Script pane to look like Figure 15.10.

Figure 15.10. The highlighted code in the Script pane indicates a problem in the script. Flash further describes the problem in the parameters area of the Actions panel. The phrase <not set yet> is a placeholder for the name of the variable to be manipulated. You must fill in the variable’s name.

Flash highlights the words <not set yet> in red to indicate that you must define the parameters of this action. The parameters for the variable appear in the parameters pane.

With Line 2 of the script selected, in the parameters area, in the Variable field, enter the variable name luckyNumVAR.

This field tells Flash where to put the value that you are about to enter. In this case, the value goes into the dynamic text box containing the words Lucky Number.

Check the Expression checkbox to the right of the Value field.

Checking Expression tells Flash that this field contains, not the value itself, but a formula for calculating the value.

In the Value field, enter the following: Number (ageVAR) + 2

This code tells Flash to add 2 to the number contained in the ageVAR variable that you created in “Creating Scriptable Text Objects” (Figure 15.11). The script takes whatever number your viewer types in the Age text box and adds 2 to it mathematically. (If the age entered is 76, for example, the result is 78.) Here, Number tells Flash to treat the data in the variable as a number, not as a string of characters. If you were to omit Number and enter just ageVAR + 2, the script would concatenate the numbers—that is, join them in one long text string (762, in this example).

Figure 15.11. This script tells Flash to put the result of the Value-field formula in the variable named in the Variable field,in this case, luckyNumVAR, which corresponds to the text box containing the words Lucky Number.

From the Control menu, choose Test Movie to try out your manipulation script.

Type a number in the editable text box containing the word Age.

Click the button.

Flash adds 2 to the number that you typed and displays the total in the text box that previously contained the words Lucky Number (Figure 15.12).

Figure 15.12. In Test mode, select the Age text box (top), and enter a number. When you click the button, Flash adds 2 to the number you entered and places the result in the Lucky Number text box (bottom).



Not a subscriber?

Start A Free Trial

  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint