Simply put, an array in Flash is a collection of data
organized under a single variable name. Let's imagine you are jotting down a
list on a piece of paper. Let's call the piece of paper "groceries". Now, in the
paper, you write a numbered list with all the items that belong there. Start
numbering from zero and enter an item that you need to purchase.
The piece of paper called "groceries" is your array variable. The items that
you need to purchase are called the array values. In Flash, the above scenario
can be represented as the following line of code:
grocery = ["bananas", "milk", "rice", "lemons"];
What makes arrays unique is that you can access individual items from an
array - similar to highlighting one item in your grocery list. Each item in an
array is assigned a number starting with zero. In the above example, bananas
would be given the value 0, milk the value 1, rice the value 2, and so on.
Any code, unless otherwise mentioned, in the following sections should be
pasted in a frame's Action window and previewed by pressing Ctrl + Enter.
Declaring an Array
There are several ways to declare an array. The easiest method, would be to use
the following format:
variable = ["item0", "item1",
If you have used other programming languages, you will notice that Flash is
less 'picky' when it comes to arrays, what type of data can be stored in an
array, the initial size of an array, etc.
Displaying an Array
Copy and paste the following lines of code into a frame and press Ctrl + Enter:
grocery = ["bananas", "oranges",
You should see the items in your array displayed. In a majority of instances,
you will be displaying and transferring individual array items instead of the
entire array. As you may have read earlier, the items in an array are organized
according to a number called an index value that starts with 0 and increases by
1 to accommodate each item stored in your array.
For example, to display the first item in your array, modify the last line in
your code from trace(grocery);
The word bananas will be displayed on the screen. To display the second item
in your array, type:
You can keep increasing the index value until you have reached a number that
surpasses the actual number of items stored in your array. In other words, if
your array only has 4 items, trying to display
grocery or grocery will result in a
message of 'undefined' - remember, an array's
index starts at 0. An index value of 4 is actually referring to 5 items
Adding Items to the End of Your Array
More than likely, you will reach a point where the items in your array
simply won't suffice. Your grocery list needs more items. The array must grow.
There are several ways to add items to your array:
One way would be to manually enter the index number of your array and assign
a value as well. If you were to, say, add an item called "potatoes" to your
current list of 4 items, simply add the following code:
grocery = "potatoes";
From now on, every time you access the grocery array, it will now include
potatoes at position 4. When items are added to an array, they stay in that
array unless you tell Flash to remove those items. We'll talk about removing
items a little bit later on in this tutorial.
Let's say you want to now add the item butter to the end of your array. You
could manually enter the index position and assign a value at that position.
Instead, we will try a more effective way. The new method of adding items to
your array would be applied by using the length function to find out the length
of the array. Once the length has been found, you can tell Flash to add an item
to the end of the array without you having to manually enter the index position.
The code for accomplishing that would be:
i = grocery.length;
In the above code, the variable "i" gets the length of the array grocery.
In the second line, the value of the length of the array is set as the index
position of your grocery array.
You should take care to note that the length function simply counts the
number of items in your array, and it does not start at zero; it starts at 1.
Therefore, the value of the length is always one more than the index position of
the last item in your array. That suits us quite well because we are interested
in adding an item to the end of our array, and not replace the last item
in our array with our new value.
Replacing Items In Your Array
Replacing items in your array is fairly simple. Let's say you aren't happy
with your second entry in your grocery list. In Flash terms, you want to replace
the second item in your grocery array with something else. The code for that
The second item, of course, has an index of 1, and the previous value for
said position has now been overwritten with "tangerines."
There is more to arrays than just the basics, and the next page will explore
more ways of manipulating data in an array.