Introducing Arrays
Arrays
This posting describes a highly important technique -- randomized selection from a series of options, using an ActionScript object called an Array. Arrays appear in most programming languages, and work quite similarly. Essentially an Array (we'll capitalize from now on, since the names of native objects in ActionScript are always capitalized) is just an ordered series, or comma-separated list.
Here's the instruction used to set up one of the Arrays we'll be using:
var wordSource:Array = new Array("ornithopter","greenstick",
"bathmat","lemnifer","perdurable","malfangle","redip",
"harry","plenitude","whitsunday","rogerian","whittle",
"whipsaw","sorghum","pennyfarthing","bleat");
Because the items in this Array are strings, they are contained in quotation marks. If they were numbers, they'd appear alone, as in this example:
var numSource:Array = new Array(1,47,516.2,34687,17,0)Note that when you create an Array consisting of strings, the separating comma goes outside the trailing quotation mark. If you're used to dealing with commas and quote marks in English prose, where the order is reversed, you may be prone to mistakes, so watch out.
Let's consider two more salient features of Arrays before looking at some examples.
First, each item in an Array is automatically numbered. You can refer to an item in an Array by its index, or number in the sequence. HOWEVER!! -- the items in an Array are numbered from ZERO, not from one. The first item has the index 0, not 1, and the last item's index is one less than the total number of items in the Array.
So wordSource, the String Array above, has 16 items. Its last item, "bleat," can be referred to as wordSource[15]. This is the syntax for referring to an Array item: name of Array followed (without space) by square brackets, and inside them, a number, or an expression that resolves to a number, such as a variable or method.
Finally, Arrays also come with one very important property called length. This is a Number variable indicating the total number of items in the Array. In our example, wordSource.length is 16. Note that, because of zero-based numbering, the length of an Array is always one more than the highest index number.
Now we're ready to look at an example.
The demonstration project at right uses a timer effect (which will be explained later in the course) to control random selection from the wordSource Array shown above. The results are inserted into a text field that is part of a Movie Clip symbol. There are five instances of this Movie Clip, all running the same ActionScript class.
In the interest of space, we'll skip the architectural details. We'll concentrate on the two lines that make the selection from wordSource:
var randy:Number = Math.floor(Math.random()*wordSource.length); outField.text = wordSource[randy];
The variable randy (the name is arbitrary) is declared with type Number. We plug into that variable our now-familiar randomization formula, with the range set to wordSource.length. Using the length property instead of a definite number allows us to change the number of items in the Array whenever we like, without otherwise touching our code.
On the righthand side of the equals sign, note the use of the square bracket notation to refer to an Array item. The variable randy, a random number, is the index.
For more about random number generation, and its relationship to Arrays, see our posting about the random() method of the Math object.
|
|