View and vote on the article here: How to Create Arrays in Flash 5
How to Create Arrays in Flash 5| Category | | | Summary | | | Body |
Those of you who banged your head against your keyboard trying to find a way to "fake" arrays in flash 4 should be very happy with the new Array object in flash 5. No more doing things like:
var1 = "timmy";
var2 = "heather";
var3 = "michelle";
Now there's real support in flash for not only one-dimensional arrays, but, as I'll show you in the next tutorial, multidimensional arrays as well. So let's get started!
First, a quick description of what arrays are for those flash actionscripters not accustomed to using them. An array is a container. It contains a bunch of elements, where each element (which is referred to by an index) has a value. Arrays in flash (like in most programming languages) are zero-based. All this means is that the first element in any array you create automatically has an index of zero. For example, if you had an array named "friends", and the value of each element of the array was one of your friend's names, you could create it in flash like this:
friends = new Array("timmy", "heather", "michelle", "bob");
Now, if you wanted to get the first element of the array and put it into a variable, you could use the following syntax:
_first_name_i_want = friends[0];
You may be wondering why we're using the number zero, not the number one to get the first element in the array. As I mentioned before, arrays in flash are zero-based, so the first element of your array is in the 0th position. Don't take my word for it. Type:
trace(friends[0]);
Into your script window and hit <ctrl><enter> to test out your scripting. If you've created the array as specified above and then used the trace command, you should see "timmy" in the output window. Using the same logic, friends[1] = "heather", friends[2] = "michelle" and friends[3] = "bob". In our array there are 4 elements, which can be referenced by the indices 0, 1, 2 and 3.
One great thing about arrays is that they can hold a lot of related information. But that information is only useful if you can do things with it. Let's look again at the friends array we created. What if we wanted to print out all the friends we have listed in our array, but aren't sure how many there are. Luckily, the Array object has built-in properties and methods. In this case, let's think about what we need to do in English first before attempting to program anything.
figure out how many elements are in the array
starting at the first element in the array
loop through the array
print out the value of each element
move onto the next element
repeat loop
Now let's try translating that into actionscript. One of the built-in properties of Array objects in flash is the length property. The length property tells you how many elements are in your array. For example, if you wrote:
how_big = friends.length;
trace(how_big);
The output would be 4. Something important to notice here is that when we're talking about sequentially indexed arrays with integer indices*, the length of the array is equal to the highest index + 1.
Now that we know about the length property, we can use a for loop to loop through the array and print out the value of each element. In this case we'll start creating our loop like this:
for(i=0; i<friends.length; i++) {
we'll be putting stuff here in a minute...
}
Let's go over what that line means. You can see in the actionscript dictionary that the syntax of the first line of a for statement is: for(init; condition; next). Let's look at the "i=0;" part in our for loop first. In this case, we have created a temporary variable called "i" and initialized its value to 0. We do that because we want to start looping through the values of the elements in the array starting at the 0th element (which, in the case of the friends array, is "timmy").
Now look at the "i<friends.length;" portion of the statement. This is the condition that determines whether or not the for loop will continue. If the condition is true, the loop will continue. If it is false, it will break out of the loop. The condition in a for loop is always evaluated before each loop interaction. In this case, the statements in the for loop block will keep being run until the value of our temporary variable "i" is no longer less than the length of our array (which, we determined earlier, was 4). NOTE: it is very important that you are careful not to make the condition part of your for loop something that never evaluates to "false". If you do, you'll create an endless loop, which is a nice way to crash your computer.
In the third part of the for statement we use "i++". the ++ operator is the increment operator. When it is called after each loop iteration, it increases the value of "i" by 1. You don't have to use this - the expression "i + 1" would have accomplished the same thing - but hey, we're here to learn new things, right? :) It's time to put the statement in the loop that will print out the values in our friends array:
for(i=0; i<friends.length; i++) {
trace("friends[" add i add "] = " add friends[i]);
}
Once again we're using the trace statement to output the values in the array. The first part of the trace statement: trace("friends[" add i add "] = " just creates the strings: "friends[0] = ", "friends[1] = ", "friends[2] = " and "friends[3] = " for debugging purposes so that we know which element's value we are seeing. Note that we're using our handy counter variable "i" as the index, which is automatically increased by 1 after each loop iteration, so it does all our work for us. The second part of the statement, add friends[i] is not in quotes. This means that we're evaluating the value of friends[i] each run through the loop (with i starting at 0 and increasing by 1 each time until its value is no longer less than 4) and then concatenating that value to our string.
OK, enough explanations. Let's look at the output of our above for loop in flash 5:
friends[0] = timmy
friends[1] = heather
friends[2] = michelle
friends[3] = bob
Now you have to admit that is pretty cool. :) So translating actionscript back into English, we see what our for loop does is check the length of our array, create a temporary counter variable called "i", initialize the value of "i" to 0 and loop through the array, printing out each element "i" of the array where "i" increases by 1 at the end of each loop until the loop condition is no longer met and it stops.
Congratulations! you've just created and output the values in your first array in flash. Next tutorial we'll look at some built-in methods for Array objects, as well as create a multidimensional array.
* That phrase probably sounds real confusing, but all it means is that, to keep things simple, the arrays we're working with here in this tutorial don't "skip over" any elements or use anything other than integers (e.g.. strings) as an index.
|
|
This article was imported from the CyberArmy University site. (original author: )
There are no replies to this post yet.
|