00:00
00:00

## AS: Arrays

5,504 Views | 40 Replies

AS: Arrays 2006-01-20 08:18:02

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

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.

Response to AS: Arrays 2006-01-20 08:20:03

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.

So yes, thats all i got about arrays, if there’s something you are wondering about, just ask.

Response to AS: Arrays 2006-01-20 08:46:17

long, and confusing :P

nice effort though, didn't read it, too long

Response to AS: Arrays 2006-01-20 08:54:07

so let me get this straight.
a array is simply a way of decreasing coding????? is that it?

Response to AS: Arrays 2006-01-20 08:54:14

At 1/20/06 08:46 AM, Inglor wrote: long,

Ya it was pretty long

and confusing :P

Confusing? in which way?

nice effort though, didn't read it, too long

Thank you! :)

Response to AS: Arrays 2006-01-20 08:55:20

At 1/20/06 08:54 AM, pumpkinlover wrote: so let me get this straight.
a array is simply a way of decreasing coding????? is that it?

no! arrays is a way of holding many values at once which can be used for alot of things.

Response to AS: Arrays 2006-01-20 08:58:16

i just dont get the use for arrays, even your tutorial doesnt really suggest there is a NEED for them.

Response to AS: Arrays 2006-01-20 09:21:54

At 1/20/06 08:58 AM, pumpkinlover wrote: i just dont get the use for arrays, even your tutorial doesnt really suggest there is a NEED for them.

you try to do flash without arrays, and I'll try to stop myself from laughing at you next time you need to store several variables that share the same concept like several names, scores, enemies and such.

Response to AS: Arrays 2006-01-20 09:25:08

It could also be added that you shouldn't overuse arrays, since Array is a complex data type (not primitive) and therefore drains more memory than the primitives. So if you can use primitive variables instead of using arrays, do so.

For example, if you have an array that stores the mouse position over a time span it's not a good idea to store it like: [[x1, y1], [x2, y2], [x3, y3], ...]. In that case this would be better: [x1, y1, x2, y2, x3, y3, ...].

Response to AS: Arrays 2006-01-20 09:28:49

At 1/20/06 09:25 AM, Rantzien wrote: For example, if you have an array that stores the mouse position over a time span it's not a good idea to store it like: [[x1, y1], [x2, y2], [x3, y3], ...]. In that case this would be better: [x1, y1, x2, y2, x3, y3, ...].

i seem to be learning something new everyday :)

Response to AS: Arrays 2006-01-20 09:28:57

wouldn't it make more sense to use the point class or create one of your own

class Point{
public var x:Number;
public var y:Number

function Point(x:Number,y:Number){
this.x=x;
this.y=y;
}
}

then create an array of point?

Response to AS: Arrays 2006-01-20 09:30:41

At 1/20/06 09:28 AM, Inglor wrote: wouldn't it make more sense to use the point class or create one of your own

class Point{
public var x:Number;
public var y:Number

function Point(x:Number,y:Number){
this.x=x;
this.y=y;
}
}

then create an array of point?

would that acctually make any difference?

Response to AS: Arrays 2006-01-20 09:36:24

At 1/20/06 09:30 AM, Creeepy wrote: would that acctually make any difference?

logically? yes

Response to AS: Arrays 2006-01-20 09:47:18

At 1/20/06 09:28 AM, Inglor wrote: wouldn't it make more sense to use the point class or create one of your own
then create an array of point?

Yes, but Point is also a complex data type. A good way to recognize them is that they usually handle two or more primitive data types, meaning that they have to make use of several data types. The memory usage difference is not that big, it's mostly that it's a bad idea to use complex data types where primitives works just as well =)

Response to AS: Arrays 2006-01-20 11:06:44

Point objects make it easier to organize/visualize imo :)

Response to AS: Arrays 2006-01-20 11:26:01

At 1/20/06 11:06 AM, Afro_Ninja wrote: Point objects make it easier to organize/visualize imo :)

Yeah, I like them too but if I were to make a big game I would use a more memory-saving solution.

Getting a bit off topic here, but Point is a fun class to extend too =)

Response to AS: Arrays 2006-01-20 14:13:00

At 1/20/06 11:26 AM, Rantzien wrote:
At 1/20/06 11:06 AM, Afro_Ninja wrote: Point objects make it easier to organize/visualize imo :)
Yeah, I like them too but if I were to make a big game I would use a more memory-saving solution.

Getting a bit off topic here, but Point is a fun class to extend too =)

i've never acctually used the Point class!
I will look it up.. : )

Response to AS: Arrays 2006-01-20 14:18:13

I never really 'used' the class either (one already exists?), I just made my own

class Point
{
var x:Number;
var y:Number;

function Point(inX:Number,inY:Number)
{
x=inX;
y=inY;
}
}

var pointArray:Array = new Array();
pointArray.push(new Point(20,30));

Response to AS: Arrays 2006-01-20 15:02:48

At 1/20/06 02:18 PM, Afro_Ninja wrote: I never really 'used' the class either (one already exists?), I just made my own

i thought it was a built-in class in flash, maybe not?

Response to AS: Arrays 2006-01-20 15:30:36

At 1/20/06 02:18 PM, Afro_Ninja wrote: one already exists?

Yes it does, in Flash 8. It comes with methods such as distance (static, finds distance between two points), normalize (adjusts the point's position to be a certain distance from (0, 0)), interpolate (returns a point between two points at a certain focal), etc.

I believe it was created for easier use of vectors, but I'm not entirely sure.

Response to AS: Arrays 2006-01-20 15:50:54

Yes it does, in Flash 8. It comes with methods such as distance (static, finds distance between two points), normalize (adjusts the point's position to be a certain distance from (0, 0)), interpolate (returns a point between two points at a certain focal), etc.

ah ok!

Response to AS: Arrays 2006-01-20 16:12:38

okay i get the concept, im sure i'll need them, but not yet, not yet.

Response to AS: Arrays 2006-01-20 17:27:10

At 1/20/06 03:30 PM, Rantzien wrote: Yes it does, in Flash 8.

well isn't that just handy

Response to AS: Arrays 2006-01-21 16:30:31

you forgot the link to AS:Main

|

Response to AS: Arrays 2006-01-21 16:38:10

using AS2 classes is actually faster just test it out yourself...

Response to AS: Arrays 2006-01-21 16:47:44

At 1/20/06 03:30 PM, Rantzien wrote: with methods such as distance (static, finds distance between two points)

are you serious, i spent 20 min's revising pythagoras theorm (i wasnt paying attention in class) to find the distance between 2 points, now you tell me there's a built in class.

========|| WWWWWWWW>[-[Blog] - [Audio] - [Userpage] - [Flash] - [Last.fm]-]<WWWWWWWW ||========

Response to AS: Arrays 2006-03-19 06:12:57

ZOMG I USED THIS KNOWLEDGE TO MAKE A SNAKE!

Make a new flash document.
put five mcs on the frame, and call the
Main, box2, box3, box4 and box5

then make a new layer, and press F6 to make a keyframe.
On that keyframe put this script;

function changepos() {
_root.snakey[5] = _root.snakey[4];
_root.snakey[4] = _root.snakey[3];
_root.snakey[3] = _root.snakey[2];
_root.snakey[2] = _root.snakey[1];
_root.snakey[1] = main._y;
_root.snakex[5] = _root.snakex[4];
_root.snakex[4] = _root.snakex[3];
_root.snakex[3] = _root.snakex[2];
_root.snakex[2] = _root.snakex[1];
_root.snakex[1] = main._x;
}
if (i != 1) {
var snakey:Array = new Array();
var snakex:Array = new Array();
i = 1;
}
if (Key.isDown(Key.UP)) {
main._y -= 30;
_root.changepos();
} else if (Key.isDown(Key.DOWN)) {
main._y += 30;
_root.changepos();
} else if (Key.isDown(Key.RIGHT)) {
main._x += 30;
_root.changepos();
} else if (Key.isDown(Key.LEFT)) {
main._x -= 30;
_root.changepos();
}
box2._x = snakex[2];
box3._x = snakex[3];
box4._x = snakex[4];
box5._x = snakex[5];
box2._y = snakey[2];
box3._y = snakey[3];
box4._y = snakey[4];
box5._y = snakey[5];
gotoAndPlay(1);

Enjoy!

I make flashes because I can.

Response to AS: Arrays 2006-07-07 11:50:16

I hate sounding like a noob, but..
why is it better to use arrays the variables??

Response to AS: Arrays 2006-07-07 11:53:35

you can do many fancy things with arrays, not to mention saving time

for example, how would you put 12 numbers in increasing order without arrays

Also, think of games like "King of Buttons" imagine the frustration of making hundreds of variables for all the comments

and plus...arrays aren't that hard to learn and IMO, are explained very well in this tutorial (in fact, this is where I learned about them)

Response to AS: Arrays 2006-07-07 12:17:44

At 7/7/06 11:50 AM, phyconinja wrote: I hate sounding like a noob, but..
why is it better to use arrays the variables??

Well, here are two quote generators:

var q0:String = "Hello World";
var q1:String = "Pie plz";
var q2:String = "1337";
trace(_root["q"+random(3)]);

With arrays:

var quotes:Array = ["Hello World", "Pie plz", "1337"];
trace(quotes[random(3)]);

See, arrays are sexier. Arrays are basically used to hold multiple, related information easily.

Sup, bitches :)