Using Variables

I l @ ve RuBoard

Using Variables

A variable is a placeholder for information. Think of it as a container. The container (variable) is always the same, but the contents (value) can change. You can change the contents of a variable to record information about what a user has done, record values that change as the movie plays, or evaluate whether a condition is true or false. You can use variables to make updating your movie easy; every place where the variable is used, Flash fills in its value.

One place you can use the value of a variable is a dynamic text box, which can display dynamically updated text such as scores and stock quotes. To assign the value of a variable to a dynamic text box, you give the variable and the dynamic text box the same name .

  1. Open zoo28.fla.

    graphics/07fig02.gif

    You should start this lesson with zoo28.fla open. It's one of the files you saved in the last lesson, so you should be able to find it in the FlashTFS folder on your hard drive. If it's not there, you can open the file from the Lesson07/Starting folder on the CD-ROM. Just make to save it to your hard drive before you continue.

  2. Select frame 12 of the Menu layer. Select the instance of the Visitor Info Button in frame 12, and press Backspace or Delete to remove it. Right-click (Windows) or Control-click (Macintosh) the name of the Menu layer, and choose Hide Others.

    graphics/07fig03.gif

    You are going to use ActionScript to create a menu in this layer, so you should remove the button now.

    Since you want to concentrate on the menu you're building, it'll be easier to work in the movie if only the Menu layer is visible. So it's a good idea to hide all the other layers in the movie so that their contents won't distract you and you won't accidentally select something in another layer.

  3. Select the text tool from the toolbox. In the Property inspector set the Text Type to Dynamic Text. Modify the remaining properties so that the Property inspector looks like the figure below.

    graphics/07fig04.gif

    When you select the text tool from the toolbox, the Property inspector should display the Text properties. The Text Type is the setting on the far left. Set the Text Type to Dynamic Text, which allows you to add a text box that displays content generated by ActionScript. Set the Font to Verdana, the Font Size to 10, and the Text (fill) Color to red (#CC0000). You also need to set the Line Type to Single Line. This ensures that the text box you create in the next step will allow users to type text only in a single line. You will add a multiline text box later in this lesson.

    Compare the Property inspector with the figure above, and make sure the rest of the settings are the same.

    Notice that choosing Dynamic Text causes the settings in the Property inspector to change. The Line Type setting becomes available, and the Variable setting appears along with the Character button. The Use Device Fonts option disappears, and the Auto Kern option becomes disabled. The left half of the figure below shows the Property inspector when Static Text is selected; the right half shows the Property inspector after you choose Dynamic Text.

    graphics/07fig05.gif

  4. Make sure frame 12 of the Menu layer is selected. Create a text box on the stage by clicking and dragging to the desired width.

    graphics/07fig06.gif

    The text box should be approximately 100 pixels wide.

    NOTE

    Be sure to resize the text box by double-clicking and dragging the handle on the right side of the text box. Don't use the Property inspector to resize the text box, as that will resize the text inside the text box. You want the text to have a font size of 10, and resizing the text box using the Property inspector may result in strange -looking text.

  5. Use the arrow tool to select the text box you just drew. In the Property inspector, type mtext in the Variable text box.

    graphics/07fig07.gif

    When you switch to the arrow tool, the text box should be selected automatically.

    The Variable setting in the Property inspector is the variable name associated with the text box. It's set in the text box labeled Var. A variable is a container for a value. The container you just created is a text box called mtext. You're going to use ActionScript to provide a value for that container.

  6. Select frame 1 of the Actions layer, and open the Actions panel if it's not already open. Click the Options menu control in the top-right corner of the Actions panel, and choose Expert Mode.

    graphics/07fig08.gif

    TIP

    If you can't find the Actions panel, choose Window > Actions to open it. You can also use the keyboard shortcut F9 to open this panel.

    When you switch the Actions panel to Expert Mode, the Actions toolbox is still on the left side of the panel, but the parameters pane disappears. A few extra buttons also appear above the script pane.

    In expert mode you can enter ActionScript directly into the script pane, much as you would add JavaScript or C++ in a text editor. You can use the Actions toolbox and the Add button to add ActionScript to the script pane. Expert mode also lets you check for syntax errors, automatically format code, and use code hintsjust click the Check Syntax, Auto Format, or Show Code Hint buttons to trigger one of these very useful features. You will use all three later in this lesson.

  7. Make sure you still have frame 1 of the Actions layer selected. Click in the script pane, and type mtext = "guestbook"; .

    graphics/07fig09.gif

    The following ActionScript should now appear in line 1 of the script pane:

      mtext = "guestbook";  

    Notice that this ActionScript appears in line 1. If you don't see the line number on the left of the ActionScript in the script pane, click the Options button near the right side of the Actions panel, and choose View Line Numbers . You should also see the line number, total number of lines, and current column position of the pointer at the bottom of the Actions panel.

    This ActionScript sets the "container" to mtext , which is the name you gave the variable in the dynamic text box. It also sets the contents of the container to "guestbook" , using the assignment operator (=). The quotes around the contents indicate that the value is a string, which is a type of data that the variable can hold.

    TIP

    You can click the Check Syntax button above the script pane, and Flash will check the code to make sure the syntax is correct. If there are any errors, a window will open, indicating where those errors might be.

    The value of a variable, or what the "container" holds, can be one of several types of data. For mtext , the variable's value is a string, which is a sequence of characters such as letters , numbers, and punctuation marks. Variables can also hold numbers, which can be manipulated using arithmetic operators such as addition (+) and subtraction (). You'll learn more about operators later in this lesson. The value of a variable can also be a Boolean, which is a value that is either true or false. Movie clips are considered data types in Flash, as you will learn later in this lesson. Variables can have a null value, which means "no value." A variable can have an undefined value, which happens when the variable has not yet been assigned a value.

    Variables can refer to objects, which are collections of properties and methods . Each property in an object has a name and value, and the value can be any data type. Each method of an object describes what the object does. For example if you had a Seal object, that object might have a method called swim. The swim method of the Seal object tells the seal to swim. You can often pass arguments, or additional information, to a method of an object, to modify how it works. Using the Seal object example, you might pass an argument called speed , with a value of 20, to the swim method. The argument would cause the seal to swim at a speed of 20 knots. You'll learn much more about objects throughout this lesson.

    NOTE

    The type of data a variable contains affects how the variable's value changes when the variable is assigned in ActionScript.

    The semicolon (;) at the end of the line of ActionScript you just added is punctuation for ActionScript. It lets Flash know you've reached the end of a line. It's not required, but it's good practice to add a semicolon at the end of a line of code. You will see that ActionScript has other punctuation, such as curly braces ({ and }).

  8. Choose Control > Test Movie.

    graphics/07fig10.gif

    When you test the movie, the word guestbook should appear somewhere on the stage. The value of the mtext variable you created in frame 1 of the Actions layer is passed to the text box with the same variable name. When you're finished, close the test-movie window.

  9. Save the file as zoo29.fla in the FlashTFS folder on your hard drive.

    The text box on the stage isn't quite a menu, but you are going to use it to create one.

I l @ ve RuBoard


Macromedia Flash MX. Training from the Source
Macromedia Flash MX: Training from the Source
ISBN: 0201794829
EAN: 2147483647
Year: 2002
Pages: 115
Authors: Chrissy Rey

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