Manipulating Variables

I l @ ve RuBoard

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:

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

  2. With the arrow tool, select the button.

  3. Access the Actions-Button panel.

  4. 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.

    graphics/15fig09.gif

    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 .

    graphics/15fig10.gif

    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.

  5. 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 .

  6. 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.

  7. 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.

    graphics/15fig11.gif

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

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

  10. 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).

    graphics/15fig12.gif

graphics/01icon02.gif Tip

  • You can use the Actions Toolbox to help you enter expressions in the Script pane. With the Value field selected in the parameters area, click the Functions > Conversion Functions category, for example, and then double-click Number . Flash enters the word number , followed by parentheses, in the Script pane and in the Value field of the parameters area. Click Operators > Arithmetic Operators and then double-click the plus sign to have Flash enter the ad ition operator.


I l @ ve RuBoard


Macromedia Flash MX for Windows and Macintosh. Visual QuickStart Guide
Macromedia Flash MX 2004 for Windows and Macintosh (Visual QuickStart Guides)
ISBN: 0582851165
EAN: 2147483647
Year: 2002
Pages: 243

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net