PDA

View Full Version : Arrays explanation



upuaut8
October 11th, 2001, 02:27 AM
Can anyone give me a little run down on calling information from an array from other movie clips and such. Please include the basics of creating an array for any Newbies who might want to read this.

Thanks ahead of time.

edited to fix typo in subject

suprabeener
October 11th, 2001, 03:09 AM
myArray=["itemone","itemtwo","itemthree","itemfour"];

trace(myArray[2]);

will yield:

itemthree

what?! three? yes, because arrays are zero based, ie the first item is item 0. you can think of it as representing an offset from the beginning, so myArray[1] means 1 item from the beginning. this can be useful in for loops:

for(j=0;j<myArray.length;j++){
&nbsp &nbsp &nbsp &nbsp text += myArray[j] ;
}

trace(text);

will yield:

itemoneitemtwoitemthreeitemfour

you might say: text += myArray[j]+" "; to get some spaces.

erm, anything not clear? oh, how about associative arrays, which in flash are actually objects:

myObject = new Object();
myObject["harry"] = "hairy";
myObject["jane"] = "plain";
myObject["chuck"] = "wood";
myObject["****"] = "use your imagination you dirty bugger";

then

trace(myObject["jane"]);

yields:

plain

aside from for loops, i find arrays most useful when i want one action to accomplish many tasks. say the variable "z" is set (somehow), i can say:

trace(myObject[z]);

and get different results based on what "z" equals. note the lack of quotes as compared to the previous trace.

the next thing would be multidimensional arrays. a big mouthful that means an array of arrays, like so:

myMultiArray = new Object();
myMultiArray["jaguar"] = ["red","fast","expensive"];
myMultiArray["ford"] = ["blue","medium","cheap"];
myMultiArray["hyundai"] = ["yellow","molasses","worthless"];

then

trace(myMultiArray["ford"][2]);

yields:

cheap

it's like having your own mini database. in fact, that's exactly what it is.

anything else?

upuaut8
October 11th, 2001, 03:41 AM
thanks

in addition.. this is my problem specificaly.
I'm looking to have a movie clip which constantly updates it's own _y possition based upon a variable which I change through out my movie. I have 30 of these movie clips which all need their own variable for _y to read from.
Also I've heard that you can name your array elements instead of using the numbers.
So this is what I did. I set up an array called "stack" which has stack [a], stack ect, all the way up to [dd].
I am attempting to have script send a value of 20 to stack[a]
how would I have it send that data to there, AND, how would I call upon that data again.

Maybe it's obvious, but the only discriptions I find on arrays talk about entering strings into them... I dont need strings I need numbers and they don't seem to be working properly.

Any help is apprecia

eyezberg
October 11th, 2001, 04:34 AM
Buy Moocks book, everything is in there :)
(sorry, lazy today...)

upuaut8
October 11th, 2001, 04:55 AM
umm... No ...please explain it. :)

I have spent far too much on books. I even have one that details arrays... I still don't understand them.
I do however understand laziness so you're forgiven Eyez..;)

upuaut8
October 11th, 2001, 11:05 AM
ok I figured out what I was doing wrong there. Much thanks for the help. :)
now can someone give me a basic run down on arrays within arrays? ;)

suprabeener
October 11th, 2001, 12:46 PM
hmm, i wonder why my posts get scrunched like that. here's what the bottom was meant to look like.

multidimensional arrays. a big mouthful that means an array of arrays, like so:

myMultiArray = new Object();
myMultiArray["jaguar"] = ["red","fast","expensive"];
myMultiArray["ford"] = ["blue","medium","cheap"];
myMultiArray["hyundai"] = ["yellow","molasses","worthless"];

then

trace(myMultiArray["ford"][2]);

yields:

cheap

it's like having your own mini database. in fact, that's exactly what it is. you don't have to use associative arrays, regular arrays will work fine in this context too.

re: the task at hand.

i assume you worked it out as:

stack[a] = 20;

you have to create your object first:

stack = new Object();

but for your purposes i think i would use a regular array rather than an associative one. then if you name your movies mc0, mc1, mc2, mc3... or something, you can go:

for(j=0,j<30,j++){
_ _ _ _ eval("mc"+j)._y = stack[j];
}

and have all the _y positions updated in one fell swoop.