PDA

View Full Version : Arrays tutorial by LuiV


Lui
09-06-2006, 10:11 PM
Wipee! My Array tutorial is finally here. This tutorial is now to run

you through the basics of arrays, just like my variables tutorial.
Arrays are very much alike variables, they do the almost the same thing

but arrays contain variables. So you may use arrays to store multiple

variables instead of having billions of variables running through your

codes.

Arrays are basically set up like this:

myarray = new Array();


That won't do much for us though, we want to store or data.
First we would have to set them up with the data.

They can be set up in many different ways.

One of them is

myarray = new Array("data1", "data2", "data3");


The one above could also be set up as

myarray = new Array();
myarray[0] = "data1";
myarray[1] = "data2";
myarray[2] = "data3";


It doesn't matter which way you do it, you will get the same outcome if

you correctly did both. Now lets try and call up our datas


myarray = new Array();
myarray[0] = "data1";
myarray[1] = "data2";
myarray[2] = "data3";
trace (myarray[1]);


Test the movie and the code should trace number 2 in our array.
Remmember that it doesn't always need to be text it can also be numbers!
Also, that when calling arrays, the values always start from 0. e.g

myarray[0], myarray[2], myarray[3] etc...

And you can also store other variables in an array by just taking the

(")s off.


myarray = new Array();
myarray[0] = variable1;
myarray[1] = variable2;
myarray[2] = variable3;
trace (myarray[1]);


To display array contents on a dynamic text field just place the name of your array on the "var" part. It will show all content in the array, but if you want to show only one I suggest you do something like this:


text1 = myarray[1];


This will set text1 (which is the variable we are going to use to show our content on the textfield) to whatever myarray[1]'s content is.

Challenge
---
Try displaying all of your array's content on one textfield and displaying every each and one of its contents. It will take time, but it will prove that you have learned how to use arrays. Another thing to try out is to contain variables in an array then display it.
---
If you do fail to do this challenge, I suggest you read through my tutorial again. I hope this tutorial was usefull!

LuiV
P.S I'm a duck!

The_Stick
09-06-2006, 10:43 PM
It looks like a good tutorial, but I have a little advice. At the top you should talk about what exactly an array is, and when it is used.

arkhan
09-07-2006, 11:11 AM
also, you didnt show any methods arrays have..like push, shift, slice, splice, join, pull, etc etc

Woolfenstien
09-07-2006, 11:16 AM
And untill you add those, no try for me.

Sorry man :(

It's just not at the same quality as your other

Lui
09-07-2006, 01:50 PM
I'll edit it, soon, but school for me.

Logic
09-07-2006, 06:04 PM
There's two things you should know:

1) The arguments array

2) The filters array

They are arrays, but they are a special kind of array that you cannot construct.

For proof, use this code:

trace(typeof (my_mc.filters))

They are different because you will find differences in the way they function when you use the array methods on them. You will also find that if you attempt this code:

trace(my_mc.filters)

You will get an output reading "Type Object". We clearly just proved they are arrays with the previous code, yet it returns they are objects. Also, if you attempt to access properties of these objects, you get more errors. Note that you can trace the arguments array though, I know, weird.

Thus, I call them "object-arrays". These are the only two arrays that I know that exist like this.

My next post will show you something cool you can do with the filters array.

arkhan
09-07-2006, 09:36 PM
arguments arrays are parsed by functions..try this

function tr(){
//for(i=0;i<arguments.length;i++){
//trace(arguments[i]);
//}
trace(arguments.join(" : ");
}
tr("hello trace function!!", 123, "yoyo"+7);

didnt test the code above, so there may be an error somewhere..but it will basicaly trace every argument you parse to the function..even tho the function does not need any arguments..

GorillaStudios
09-11-2006, 08:39 AM
Theres two array tutorials on the Newgrounds BBS.

AS: Arrays by Denvish (http://www.newgrounds.com/bbs/topic.php?id=296610)

AS: Arrays by Creepy (http://www.newgrounds.com/bbs/topic.php?id=417714)

There a little more in depth, they also contain valuble information as what the other user suggested, like push, shift, slice, splice, join, pull, etc etc

AS: Main is full of information for actionscripters. Visit here (http://www.newgrounds.com/bbs/topic.php?id=229808&page=99999).