Darn i can leave you do everythingwait let me see what to add ok here are arrays
Arrays
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);
In this case it will display [january] in the output window. If you change with it will display [march]. If you choose 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 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 = “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 = [“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 = “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.
Adding values
Very often when using arrays you will get the use of adding more parts inside an array. You have different ways of doing this to be sure you add the value on right place in the array. Yougot differnet commands for choosing where to put the value, the most used one is the push() command, this will add a value in the end of the array. So if you write this to our month array above:
threeMonths.push(“april”);;
this will make our array have 4 different parts. You can also choose to add a value in the beginning of the array, then you will use the command unshift(). You use the command like this:
threeMonths.unshift(“june”);;
This command will make all the currently values uniqe numbers will increase by one, so the new value will take the first place in the array and steal the first uniqe number of the array (0) from the one before we made this. So now this (june) will be number one in the array and december,october will be number 2. You can also add values in the middle of an array. In this example i want to make a new array first then put a new value in the middle so first i will show you how the splice() commnd work, the comman dwe’re gonna use to put a new value in the middle of an array then i will show you an example.
arrayName.splice(insertion point, delete value, new value);;
if you dont wanna delete or replace a value in the array just leave the delete value blank by type a 0 there. So if you see at this example you will understand it better.
var myArray:Array = new Array(1, 2, 3, 4, 5);
myArray.splice(2, 0, 6);
trace(myArray);;
If you test it the output window will display [1,2,6,3,4,5]. The splice command can be used for both removing and adding values, read more about removing values below.
You can also use a command called concat() to put two different arrays together to one.if you got two diffent arrays you can put them together like the example below. But you will have to make a new array that contains both of the array values.
var numberArray:Array = new Array(1, 2, 3);
var alphaArray:Array = new Array(“a”, “b”, “c”);
var concArray:Array = numberArray.concat(alphaArray)
trace(concArray);;
This script will return [1, 2, 3, “a”, “b”, “c”].
Removing values
Often when using arrays, you need to remove some of the parts inside the array. There are several ways to do this. You can choose which of the values in the array you wanna delete, to remove the first part or value in an array you must use the command shift() So here’s an example:
var numberArray:Array = new Array(1, 2, 3);
numberArray.shift();
trace (numberArray);;
This will display [2,3] in the output window. There is other methods to do it on too, you can remove the last value in the array by using the function pop(). So here’s an other example below.
var numberArray:Array = new Array(1, 2, 3);
numberArray.pop();
trace (numberArray);;
this will display [1,2] in the output window. Now we only need a command to remove a value in the middle of the array. Then we will use the same function as the one we used in the ’Adding Values’ chapter. We’re gonna use the splice() function. But this time we’re gonna leave the place where we should add a number blank. So here’s hows it works now:
arrayName.splice(insertion point, delete value);;
So this work the same as last time exept we dont add a new value. So here’s an example on how it works:
var numberArray:Array = new Array(1, 2, 3);
numberArray.splice(1, 1);
trace (numberArray);;
This will deisplay [1,3] in the output window. So thta’s what we have about removing values.
Manipulating arrays
Sometimes we dont only want to change a value, but we wanna change the whole array. There is alot of different functions to do that. We can use the function reverse() to reverse the order of the values in the array. Like this:
var numberArray:Array = new Array(1, 2, 3);
numberArray.reverse();
trace (numberArray);;
This will display [3,2,1] in the output window. Another function is the function sort() to make the order in alphabetic or numeric order. So an example:
var numberArray:Array = new Array(4, 2, 3);
numberArray.sort();
trace (numberArray);;
This will display [2,3,4] in the output window. There is one more sortings function called sortOn(). This function will sort out an array in alphabetic or numeric order, but you can choose what type of value in the array you want to put in order. So if you got an array with alot of different values, you can choose to only sort that value. You can also use the function join() to make all the values in the array to stings values, and you can also change a sepearator between the values, here’s an example:
var numberArray:Array = new Array(‘a’, “b”, “c”, “o”);
trace(numberArray.join(" – "));
This will display [a – b – c – o] in the output window. So thats what i got about manipulating arrays, hope you understand it.