Using Arrays

I l @ ve RuBoard

Arrays are lists of variables . Each "line" of the list can contain a single variable, the value of which can be anything (string, number, object, another array, and so on). After you have created an array, you reference a line of the array by giving the name of your array and then the line number, which is actually called the index and always starts at 0, inside square brackets like this:

  MyArray[2];  

In the next exercise, you will create an array to contain all the items you are going to have in your menu. You should start this exercise with zoo29.fla open .

  1. Select frame 1 of the Actions layer. In the Actions panel, modify the code so you have a variable called menu, with a value of new Array(); .

    graphics/07fig11.gif

    The ActionScript attached to frame 1 of the Actions layer should now look like this:

      menu = new Array();  

    This sets the value of the menu variable to a new Array object. Remember, an object is a collection of properties and methods . An Array object is a collection of properties and methods that refer to an array. An array is a list of variables, or elements, and each element in that list is considered a property of the Array object. The Array object also has a length property, which lets you know how many elements are in the array.

    The Array object is one of the many objects built into ActionScript. An ActionScript has several methods, or special functions, that let you handle an array. For example, the Array object has a reverse method, which lets you reverse the order of the elements in the array.

    The new portion of the ActionScript is a special operator that lets you create a new object. The operator is typically followed by a constructor function, which is a special function that defines the properties and methods of a new object. The constructor function in this case is Array() , which refers to the built-in Array object. So in essence, this code creates a new copy of the Array object. The constructor function can optionally have arguments passed to it in the parentheses. In the Array object, the optional arguments are the length of the new array or the elements you want to add to the new array. The name of the new object is menu . The menu object has all the built-in properties and methods of the built-in Array object.

    TIP

    The code in this step has a space before and after the assignment operator ( = ). This space is not required, but it does make it easier to read the code.

    graphics/07fig12.gif

    As you typed the code in this step, you may have noticed that a Tooltip popped up after you typed the open parenthesis ( ( ). That Tooltip is a code hint, which displays the complete syntax for the code that Flash thinks you're typing. Notice that there are two arrow buttons in the Tooltipthese appear when there's more than one possible syntax for the code. Click the arrow buttons to scroll through the possibilities. When you type the closing parenthesis ( ) ), the Tooltip should disappear.

    NOTE

    If for some reason you didn't see a Tooltip, click the Options menu control in the Actions panel and choose Preferences. Make sure the Show Code Hint option is selected, and click OK.

    NOTE

    If you find that the code in the Actions panel is too small to read, you can modify its size. Click the Options menu control at the top-right side of the Actions panel and choose Preferences. You can then change the font size in the Text area of the Preferences dialog box.

  2. Type "home", "featured creature", "visitor info ", "map", " games ", "guestbook" inside the parentheses.

    graphics/07fig13.gif

    Your ActionScript should now look like this:

      menu = new Array("home", "featured creature", "visitor info", "map",   "games", "guestbook");  

    When you use the Array() constructor function, as you did in step 2, you can type either the length of the new array or a list of the elements inside the parentheses. Or you can leave the parentheses empty. In this case, you added six elements to the array: "home", "featured creature", "visitor info", "map", "games", and "guestbook". Notice that each of the elements in this array is surrounded by quotation marks, which indicates that each element is a string.

    TIP

    The space between each element in the array is optional, but it does help make the code more readable.

  3. Add a second line to the script pane, and type mtext = menu[0]; .

    graphics/07fig14.gif

    You can add a second line by going to the end of the first line and pressing Enter or Return. Then just type the code. Your ActionScript should end up looking like this:

      menu = new Array("home", "featured creature", "visitor info", "map",   "games", "guestbook");   mtext = menu[0];  

    Once again you added the mtext variable, but this time its value is quite different. Instead of setting the value to a string, you set it to one of the elements in the menu array, namely menu[0] . This refers to the element in the menu array that has an index of 0, which is the first element ( "home" ). The second element in the array ( "featured creature") has an index of 1, the third has an index of 2, and so on.

  4. Choose Control > Test Movie.

    graphics/07fig15.gif

    When you test the movie, the word home should now appear in the dynamic text box that has the variable name mtext .

    Close the Test Movie window when you're finished.

  5. Select frame 1 of the Actions layer, and modify the ActionScript so the mtext variable has a value of menu[3] .

    Your ActionScript should now look like this:

      menu = new Array("home", "featured creature", "visitor info", "map",   "games", "guestbook");   mtext = menu[3];  

    Now the value of the mtext variable is another element in the menu array. This time it's the one that has an index of 3, which is the fourth element ( "map" ).

  6. Choose Control > Test Movie.

    graphics/07fig16.gif

    This time, when you test the movie, the word map should appear in the text box.

    Close the Test Movie window when you're finished.

  7. Select frame 1 of the Actions layer, and modify the menu array so each element is on its own line.

    graphics/07fig17.gif

    This is actually much easier than it sounds! All you have to do is delete the arguments (all of the elements) in the Array constructor function, so the first line of ActionScript will look like this:

      menu = new Array();  

    Then you can add a new variable name/value pair for each element. The variable name is the name of the array, followed by the index number of the element inside of square brackets ( [ and ] ). The value is the element. Don't forget that the index numbers start at 0, and make sure to put quotes around each value so Flash knows it's a string. For the first element, you would type this:

      menu[0] = "home";  

    Your ActionScript should be:

      menu = new Array();   menu[0] = "home";   menu[1] = "featured creature";   menu[2] = "visitor info";   menu[3] = "map";   menu[4] = "games";   menu[5] = "guestbook";   mtext = menu[3];;  

    There's no need to test the movie again, unless you want to double-check your work. You didn't actually change the array, you just split it into several lines, making it a bit easier to read and modify. You will make some major changes to the menu array in Lesson 11, and this step is preparation for those modifications.

  8. Save the file as zoo30.fla in the FlashTFS folder on your hard drive.

    You still don't have a menu, but you have an array that contains all of the text for your menu. Over the course of the next two exercises you'll use more ActionScript to dynamically add several instances of a movie clip, each of which will contain one element from the array.

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