View Full Version : Arrays - Basic Question
Hello all,
I'm still fairly new to Flash but think I have a grasp of the basics of actionscript.
However, the one thing I can't really understand is the 'array'.
I know that they are used to store information and I'm sure that they're incredible useful - I just don't know how!
If anyone can give some examples of when they use arrays in their scripts and for what reason I'd be grateful and do a bit of experimenting.
Cheers for any advice / ideas,
David
Sinister Rouge
12-30-2006, 07:50 AM
Think of an array as a one column spread sheet. For instance I could have an array called Names within that array I could have
John
Beth
Rich
etc.
all of these are contained in a single item, so using code I could loop through the array Names and print one or all of the names to the screen or do any number of other things to it.
you can also have multi dimenstional arrays which would allow for something like this.
Array Name "Cars"
--> Color // you can't actually name these arrays to my knowledge but you can set them up so they are all under cars.
--> Red
--> Blue
--> Model // you can't actually name these arrays to my knowledge but you can set them up so they are all under cars.
--> Honda
--> Toyota
By accessing the cars array, I could also access the sub arrays model and color... Basically they are a progromatic spread sheet. Matrices for instance are just multidimensional arrays.
I see... but what practical use would you have for all of the information in the array?
How would you use this information in your script?
I think this is what I'm struggling to come to terms with!
Cheers for the help - much appreciated,
David
Sinister Rouge
12-30-2006, 08:24 AM
because you can loop through them, they are often used for loading multiple movies with similar names like clip1, clip2... clip50. Without an array you would have to create variables to store the path to all 50 clips with an array you can use a for loop that simply loops through the array 50 times, incrementing the clip number (clip(x)). Saving probably 45+ lines of code.
Sinister Rouge
12-30-2006, 08:27 AM
Try some of these links...
Link 1 (http://www.kirupa.com/developer/actionscript/finding_values_array.htm)
Link 2 (http://www.kirupa.com/developer/actionscript/array_max_min.htm)
Link 3 (http://www.kirupa.com/developer/actionscript/multiDimArrays_attach.htm)
Link 4 (http://www.kirupa.com/developer/actionscript/array.htm)
Array's are wonderfull things... they make up around 95% of my variables normally...
I use them for ... lists.... like in my game you can pick which 'map' you want to play, a drop down list of maps appears.... i have one array with names and one with numbers... when i hit the first item on the list, the first item on the number array get's activated....
I also use them (used them... havn't figure it out yet on AS3) for map tiles... because you can call them dynamically... like map['tile'4+5] which ads the 4 and the 5 to look for tile9.... which is very usefull for complex things...
Brilliant...
I think that I have been using mulitple variables (_global variables etc) throughout my scripts where, as you say, a single array would have done the job.
That's given me some good ideas to experiment with - especially the code for accessing the information held in the array...
Thanks again,
David
Sinister Rouge
12-30-2006, 08:47 AM
also on another note, multidimensional arrays or matracies are a key componant for emulating 3D in flash. As you gain experience you will grow to love them...
Ah and girls will love it when you talk about algorithms that manipulate your map matrix......
or not... i'm to geeky... but it doesn't matter, i'm married already :D
nathan99
12-30-2006, 08:54 AM
Yeah arrays can be thought of, to an extent, as just a whole bunch of variables. By this, I mean you can do;
var myArray:Array = []; // Create a blank array, with the name of myArray
myArray[0] = "1";
myArray[1] = "2";
myArray[2] = "3";and then retrieve them the sametrace(myArray[0]);
It gets more complicated when you do;
var myArray:Array = ["1", "2", "3"];
trace(myArray[0]);although that code is EXACTLY the same as the one I just wrote, it seems a lot tougher to grasp.
The main things you need to understand is that;
1. Array values have indexes; That is their position in the array (0 = the first item, 1 = the second item, etc..)
2. Array's indexes start from 0 and count up from there
3. Array's values can be modified the same as variables; ie:arrayName[index] = "value"
4. Array's have a lot of other functions (Fully understand arrays before this)
The last note, is; There are also various types of multidimensional arrays in actionscript, attempt these when you are absolutely comfortable with arrays though
Hope that sheds a little light
nathan99
12-30-2006, 08:56 AM
btw -Z- map['tile'4+5] :/
map['tile'+(4+5)]
you mean?
Cheers everyone - as ever, the information and advice on this forum is fantastic and has been a massive help...
David
bodyvisual
12-30-2006, 01:46 PM
i wrote a really long explanation about arrays here (http://www.billybussey.com/forum/viewthread.php?tid=17488)
edit: wups, just noticed that it's for mx... :ponder:
Krilnon
12-30-2006, 07:09 PM
in this sense they're like objects, only a little more limited
Arrays aren't more limited than Objects at all, though. You can look at it conceptually or with an example:
-Array is a subclass of Object, so conceptually it should be at least as functional/useful as the Object class, though more specialized.
-Everything that can be done with an Object can be done with an Array:
var myArray:Array = ['a', 'b', 'c'];
myArray['-1'] = 'Element with "-1" as a key';
myArray[0] = 'First Element';
myArray['3'] = 3; //Counts toward length
myArray.prop = 'A property';
trace(myArray.length); //4 -- specialization, only integer keys are counted as Array elements
trace(myArray['-1']); //Element with "-1" as a key
trace(myArray['prop']); //A property -- just like an Object
trace(myArray); //First Element,b,c,3
for (var prop:String in myArray){
trace(prop+':'+myArray[prop]);
}
/*
prop:A property
3:3
-1:Element with "-1" as a key
2:c
1:b
0:First Element
*/
Hello again...
Well thanks to the help on this thread I've successfully used an array in a script.
I have text in an XML file which I load into my movie and store as two arrays.
I then have 'next' and 'back' buttons which toggle through the array and display the corresponding information - thanks again for the help.
Here's my code -
The XML text is stored as t1, t2, i1 and so on.
var numT:Number = 0;
var numI:Number = 0;
var t:Array = [t1, t2, t3];
var i:Array = [i1, i2, i3];
ttl.text = t[numT];
txt.text = i[numI];
next_btn.onRelease = function(){
numT++;
numI++;
ttl.text = t[numT];
txt.text = i[numI];
}
It works well - however, I have a new problem...
Because the string of text is loaded from an XML file, I can't seem to use the usual tags that I use to format text (<br/> and so on). I have tried using \n, but this doesn't work either. Can anyone help?
David
Done it...
Turns out that I just need to press [enter] in my XML file and that sorts the problem!
All that time wasted messing about with tags!
devonair
12-31-2006, 12:13 AM
I put together a little look at some new AS3 array methods here: http://www.onebyonedesign.com/tutorials/array_methods/
Yeah, it's AS3, but it can give an idea of what arrays can be used for, and the type of junk you can do with them..
bodyvisual
12-31-2006, 01:30 AM
^ really nice.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.