I know this is already done by Denvish,
but i made a bigger and better tutorial explaining all about Arrays.
So hope this is well explained :)
Introduction
Arrays is possible one of the most useful things in programming. How often don’t you wanna make a game with a lot less variables. Instead of having 300 lines with different variables, you can have all the variables inside an Array. Then you can controll and have access to anything on that array. This will make the game much easier to code. So in this tutorial I have trying to ake everything about arrays clear. So please sit down and enjoy this powerful tutorial.
What is an Array?
Arrays is a kind of a variable which can contains more than one value. You can edit your array and have full control over it under runtime. Arrays is a nice way to have hold and access to many variables at once and is nice for big games like an RPG or a plattformer. An array is also very effective to use combined with different math structures. Lets take an example; Snake. Snake is a cool game where you can steer your snake and its getting longer and longer each time you pick up some ”food”. The only thing flash do is to control the first part of the snake, all this movements are putting in an array. So the array always updates which movements the first part of the snake got to the y and x position and the rest of the part will just follow that information inside the array about which way they shall go. For sure it exists other methods to code a snake game.
Creating Arrays
There is several ways to creat an array. Its enough to just define the array without telling it what it shall contain. I guess you know much about variables when you read this and you should get used to use Strict Data Typing also when creating arrays. So this is the basic structure to an Array:
var arrayName:Array = [];
This will make an empty array which doesnt contain any information. I will recommend to define arrays on a different way. You should follow this way:
var arrayName:Array = new Array();
Both of this two examples does the same, it creates a new array with name, ”arrayName”. Array names are case sensitive and every name must be uniqe, so dont name it the same as a variable, function or class. And Array can contains all types of variables (numbers, booleans and strings). You can also define the array with information, like this:
var arrayName:Array = new Array("string", true, 5);
trace (arrayName);
If you test that script it will return: [string,true,5] in the output window. The comma (,) split the different parts in the array. Each part got a uniqe number. Arrays are zero-based, this means that the first part (in our case "string") got the uniqe number 0. And the next part (true)got the uniqe number 1. Now we gonna work more about reading parts of or whole arrays
Reading Arrays
Many times you will have the need of reading somehing from an array. This way you can also use it in statments to check if a part in the array is the same as something. Lets make an example now so you can try to understand better the part of reading the arrays.
var threeMonths:Array = new Array("january", "february", "march");
Lets say we in this case wanted to read the second part of the array, then we could use the trace comand which displays a message in the built-in output window in Flash.
trace(threeMonths);
If you so this script it will display [january,february,march] in the output window. You can also just trace different parts, like this:
trace(threeMonths[0]);
In this case it will display [january] in the output window. If you change [0] with [2] it will display [march]. If you choose [3] or higher it will display ”undefined”, because there is no variable in the array with that uniqe number, the highest number is 2. You got a function called lenght that displays the lenght of the array, for example:
trace(threeMonths.lenght())
This will display [3] because the threemonths aray contains of 3 different values.
Changing Arrays
Sometimes you will need to edit a spesific part in the array. Forexample you can replace the first value in our month array to be December, Its very easy to change a part in an array, just simly do like this:
threeMonths[0] = "december";
One array part can contains more than one value, forexample you got one array part that you want to contain both december and november you can do it like this:
threeMonths[0] = ["december", "november"];
Then both of the months got their own uniqe value; 0. To change the second value inside the first part ("november") you must choose the second part inside the first part of the array so to change it do it like this:
threeMonths[0][1] = "october"
Now the array ”threeMonths” first part would contain the strings [december,october] So hope you get the point with using parts inside other parts in arrays, this is a smart way of holding information that needs more variables to make sense.