Using Arrays

[ LiB ]

You can do much with arrays once they're created and initialized to some value. In this section, you're going to start out by simply finding out the length of an array and then get more advanced by examining array stacks.

Let's start:

 tempArray = [28, 35, 48, 13, 7]; trace(tempArray.length); 

This example will display the number 5. Why? Because there are five elements in tempArray and this value can be read through the special built-in variable named length this goes for any array. The dot notation notates that it is a property of tempArray .

Sometimes it is useful to figure out the length of an array when manipulated on-the-fly . One place where the length of array can be efficiently used would be in a loop. Remember in the previous example when we looped 50 times because the loop condition was set to 50 and also because it was the length of the array? What do you do if you don't know the length of the array? Well, you could do this:

 for (var index = 0; index < tempArray.length; index++) {     ... } 

Cool, no? This way, you can have that array be any length and you won't have to worry about creating new elements if the array is too short, or not accessing all of the elements if the array is too long.

The Push and Pop Commands

Now I'll show you two very useful commands, push and pop . Sounds like candy , but they're even sweeter when you need them.

The push command essentially adds an element of the specified value on top of the last element on the array (or stack). For instance, look at the following example:

 tempArray = new Array(); tempArray.push("USA"); tempArray.push("BMW"); tempArray.push("NYC"); 

The first familiar line declares the array. The following three lines add USA as the first element (index 0), BMW as the second element (index 1), and NYC as the third element (index 2).

If you were to use the push command again, it would add another element after the third element. Try messing around with the code. Use the trace command to help you better understand it. Try figuring out the length of the array before and after you use the push command. You'll see the number of elements affect the length of the array right before your eyes!

Let's work on our previous example. This time though, I'm going to show you how to use the pop command.

 result01 = tempArray.pop(); result02 = tempArray.pop(); 

Assuming the last element in tempArray contained the value of NYC, result01 would also equal that value. Why? Because the pop command returns the value it is removing from the top of the stack. That's exactly what the pop command was made to do, and it does so fabulously. See Figure 5.4 for a visual of the pop command. See Figure 5.5 for a visual with the push command.

Figure 5.4. Removing the last element in an array

graphic/05fig04.gif


Figure 5.5. Concatenating an element to the end of an array

graphic/05fig05.gif


NOTE

TIP

When removing a value, the pop command also returns the value.This allows you to use it before throwing it away. Remember that the pop command also reduces the array length by one.

At this point, you can pretty much guess what the following line (in the last example) doesit removes the last element and spits it out into result02 . Assuming the value was BMW, result02 will be BMW and the array's length will be one less.

Take a look at the following example; it demonstrates how to remove the first element in an array:

 myPets = ["cat", "roach", "snail", "kangaroo"]; shifted = myPets.shift(); trace(shifted); 

If you were to check the length of myPets , you'd see that the modification is reflected here also. I bet you're starting to get the hang of this array thing; you probably already know that if there is a method to remove the first element, there is probably a method to add an element before the first oneand you're right!

The unshift Command

You can use the unshift method of the array object to add new elements in front of an array. In other words, if you needed three new elements in front, you could add them with this command. Let's see it in action:

 LengthOfarray = myPets.unshift("spiders", "rats", "hamsters"); 

If the myPets array used to contain cat, roach, snail, and kangaroo, it now contains spiders, rats, hamsters, cat, roach, snail, and kangaroo. Yes, in that order.

The unshift method of the array object returns the length of the new array that it constructed . See Figure 5.6.

Figure 5.6. Adding an element to the front of an array

graphic/05fig06.gif


The slice and splice Commands

There are a couple of other methods that many novice programmers seem to confuse; the slice and splice commands. They sound as if they are out of an old martial arts flick, and they can inflict just as much damage if not used correctly. I will point out the distinct differences so you can be on your way to reaching your master status.

The slice command returns a subsection of an array without modifying the original array. This is great when you want to work on a particular piece of an array without touching the main one.

 myPets = ["cat", "roach", "snail", "kangaroo"]; subArray = myPets.slice(1, 3); 

This returns roach and snail in a two-element array.

Now listen carefullythe first parameter in slice indicates the index number where you want to start extracting. The second parameter indicates where you want it to stop. Listen even closerthe element indicated by index 1 will be included in the return array, but only up until the index indicated by the second parameter. In other words, the element indicated by the second parameter will not be included in the subarray.

NOTE

TIP

If you omit the second parameter to the slice command, the command will return everything from the index indicated to the end of the main array. See the Help file for more examples.

So what does slice's brother splice do? In Macromedia's documentation, the command is defined as:

 myArray.splice(start, deleteCount, value0,value1...valueN) 

Before discussing the definition I should tell you that the main difference between slice and splice (besides the spelling) is that splice modifies the array it's working on and slice doesn'tgot that?

The start parameter tells splice where to start deleting or inserting the values. deleteCount decides whether to delete or insert. Insert there if you don't want any elements deleted. After this parameter, the remaining parameters are optional. Any values inserted hereafter will be pushed into the array starting at the position indicated by start .

NOTE

NOTE

Don't think I'm driving away from the fun graphics. Arrays are heavily needed.You will see why soon.

[ LiB ]


Game Development with ActionScript
Game Development with ActionScript
ISBN: 1592001106
EAN: 2147483647
Year: 2004
Pages: 162
Authors: Lewis Moronta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net