Everything I have explained in the
previous page must seem like disparate pieces of a puzzle. For
a better understanding of what has been done, copy and paste the following code
into a frame's Actions Window. Press Ctrl + Enter after the code has been
pasted, and take care to notice how the values of your array are modified and/or
[ notice how the array is modified and expanded in your output
In the next few sections, we will discover more things to do with arrays that
go beyond the basics that you have been dealing with in the prior sections.
Creating an Array by Combining Two Arrays
Flash allows you to create a new array by combining the values of two other
arrays. Let's take, for example, the following two arrays:
colors = ["blue", "yellow",
shape = ["square", "triangle", "circle"];
We can combine both of the above arrays into one array by using the concat()
function. If we want to combine the values of the arrays colors and
shape into a new array called drawing, the code would be:
drawing = colors.concat(shape);
You should note that the values in the array shape will be added AFTER the
values in the array color. In plain English, the above code would read like "the
color array will be added to by the shape array."
If you wanted to reverse the order, you would simply change the above code
drawing = shape.concat(colors);
For example, if you were output the array 'drawing' from the above example,
you will see the following data:
As expected, the values in the array shape come first, and the values in the
color array trail behind.
Creating an Array using Slice()
What Flash bringeth together, Flash taketh apart. That's exactly what the
slice() method does. Previously, you used the concat() method to bring the
values of two arrays into a new array. Slice() does the opposite by creating a
new array by extracting a specified range of values from another array.
For example, let's take the following array:
countries = ["germany", "italy",
"norway", "finland", "united states", "india"]
If I were to create an array using only a few of the values from the above
array, the code will look like the following:
country2 = countries.slice(2,4);
What I am doing is telling Flash to extract the values from index positions
through up to four but NOT including the fourth element. For example, country2,
after running the above statement will have the values norway and finland, the
2nd and 3rd index positions respectively.
|When dealing with
concat(), make sure you
understand that the actual arrays are not modified in any way.
Any modifications you make are displayed in a new array you
create. The original source array remains unscathed.
The following section will explain the use of
other methods that will modify your array
Pop and Push
The final four methods this tutorial will cover are the
This method removes the last item from an array. While the item is removed,
you can capture that value into a variable:
seasons = ["summer", "winter",
capture = seasons.pop();
After running the above code, the pop function removes the last value
autumn from the array named seasons and stores it in the
variable capture. If you do not wish to store the last variable in your
array, you can simply use seasons.pop()
without having "capture =" in front of it.
The push method adds a series of values toward the end of an existing array.
programs = ["Flash MX", "FireWorks
programs.push("DreamWeaver MX", "FreeHand MX");
While the original programs array only contains two values, after using
the push command, the values specified by the push function are added to the
END of the programs array. In the end, the programs array now has four items
Shift and Unshift
These two functions are very similar to the the above pop and push methods.
The primary difference is that shift and unshift work from the beginning of an
array at index position 0. Remember, pop and push deal with the end of an
If I wanted to remove a value from the beginning of our programs array,
I would simply use the following code:
Of course, if you want to store the value you deleted using the shift method,
simply set a variable name equal to the above code, and the item removed will
be stored in that variable.
This function allows you to add a value or series of values to the beginning
of your array. For example, if you wanted to add two programs to our programs
array, the code would be:
Unshift does not overwrite existing values in your array however. Instead, the
unshift function shifts the existing items up the required number of index
positions to accommodate the new values.
I hope this tutorial helps you to better understand the world of arrays in
Flash. Arrays are not the most exciting of topics to write about, and they
don't involve cool animation, and they don't even require the use of cool
images. With that said, arrays are among the more important of techniques that
you will use not only in Flash - but other languages as well.
If you have a question about this or any other topic, the easiest thing is to drop by our forums where a bunch of the friendliest people you'll ever run into will be happy to help you out!
THE KIRUPA NEWSLETTER
Get cool tips, tricks, selfies, and more...personally hand-delivered to your inbox!
( View past issues for an idea of what you've been missing out on all this time! )