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
expanded:
[ notice how the array is modified and expanded in your output
window ]
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",
"green"];
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
to:
drawing = shape.concat(colors);
For example, if you were output the array 'drawing' from the above example,
you will see the following data:
square,triangle,circle,blue,yellow,green
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.
|
Note |
When dealing with
slice() or
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
pop(), push(),
shift(), and
unshift() methods:
- pop
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",
"spring", "autumn"];
capture = seasons.pop();
trace(capture);
trace(seasons);
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.
- push
The push method adds a series of values toward the end of an existing array.
For example:
programs = ["Flash MX", "FireWorks
MX"];
programs.push("DreamWeaver MX", "FreeHand MX");
trace(programs);
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
in it!
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
array.
- shift
If I wanted to remove a value from the beginning of our programs array,
I would simply use the following code:
programs.shift();
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.
- unshift
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:
programs.unshift("Director MX",
"Contribute");
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.
Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!