This tutorial may be complex for a programming novice
In a previous tutorial we looked at the use of Arrays in As3 and how we can incorporate them into and games and applications.
In this tutorial we will look at how the more complex arrays, aka nested arrays work and why they are useful
Here is an example of a simple Array:
var myArray:Array = new Array ([100],[50])
And here is an example of a Nested Array:
var myNestedArray:Array = new Array ([ [100], [500] ], [ [200], [100] ])
I know that it looks very complex but once you understand the syntax of the array it becomes a lot easier.
If you look closely at the array, you will notice that it is actually split into 2 separate parts.
The first being:
[ [100], [500] ]
The second being:
[ [200], [100] ]
If you were to imagine that the 2 end square brackets on each part were actually parenthesis the entire array would look like this:
( ( [100], [500] ), ( [200], [100] ) )
Hopefully now this is starting to make a bit more sense, a nested array is basically many arrays placed one after the other as a single array. All the square brackets that we imagined as parenthesis just separate the arrays from each other.
If you’re still confused let me show you how you access the values in the code:
var myVariable:Number = myNestedArray[0] // [1]
In order to retrieve just one variable from this array you must include the second number, in this case [1], however for this example i am commenting it out.
I was talking earlier about the nested array being split into 2 parts. The first number we use selects this part. In this example myNestedArray[0] would retrieve the first part of the array,
[ [100], [500] ]
Now. If we again imagine that the end square brackets are parenthesis we are now left with a regular array like this:
( [100], [500] )
This is what the second number is for. As we used [1] it selects the 2nd value which in this case is 500
this means that in this case the uncommented value of myVariable would be 500.
I know that this was an extremely complicated tutorial, if you have any questions on this please feel free to leave them in the comments and i will gladly answer them.