Multi-Dimensional Arrays and attachMovie
       by b.rich  |  3 August 2005

This is the continuation of our tutorial from the previous page. On this page I will explain the ActionScript behind our code:

memberInfo = [ [ "Kirupa", "Flashing (MX) complete strangers in 32-bit color.", "//www.kirupa.com/forum/member.php?u=1" ],[ "B.Rich", "Basketball, Golf, Web Media.", "//www.kirupa.com/forum/member.php?u=4513" ],[ "Senocular", "Fun stuff, like Flash", "//www.kirupa.com/forum/member.php?u=2867" ] ];

This is our array that contains all of our data. Notice that there are three main array items represented by the green, blue, and orange colored sections. Each of those colored sections are arrays themselves. For example, the name 'Kirupa' is the first item in the sub-array, the description is the second item, and the third item is the URL for that data.

Notice that the rest of the arrays follow the similar format. For example, tracing memberInfo[2][0] will out the value 'Senocular'. Flash (and other languages) will interpret the call as the first item in the third sub-array. Remember, array data positions start numbering from 0. For more information about arrays, read Kirupa's tutorial here: http://www.kirupa.com/developer/actionscript/array.htm


var xPos = 150;
var yPos = 60;

xPos & yPos are the variables which hold our starting x & y coordinates for the very first container movie attached in the loop. You can adjust these values to any spot you would like the first container to be placed.


for (i=0;i<memberInfo.length;i++) {

This is the for loop which makes our movie work. Variable i is set to 0 so it begins at the first element in our array. memberInfo is the name of our multidimensional array that holds all our our information. memberInfo.legth specifies the numerical value for the length of our array which is 3.

We use the length property so our loop will only run as many times as the number of items in our array, so if you add more items to the array you will not have to adjust the loop. i++ just tells the for loop to auto-increment the variable i each time the loop is accessed.


attachMovie("container", "new"+i, i, {_x:xPos, _y:yPos});

This line of code brings the container movie clip out from hiding in our library and to the forefront on the stage. Notice that the newly attached movie's name is "new"+i. That ensures that each movie's name is different and varies based on what the value of i during the loop is.

For example, the first attached movie clip will be called new0 because the i in our for loop is initially zero. The next movie clip will be called new1, the next new2, and so on.


yPos += this["new"+i]._height+5;

This line simply adds 5 pixels each time we attach a movie so the next movie is spaced out a little below the previous one. Notice that I use the this call to allow for dynamic variables. Remember, our attached movie clips are called "new" + i. If we want to make any modifications to that movie clip, we can either make a call to new0, new1,..., newN explicitly by writing each line out, OR we can let Flash sort it out by using this["new"+i] instead.

When you are cycling through your for loop, this["new"+i] is equivalent to new0, new1, new2, etc. depending on what your value of i is. Also, in case you are wondering, you can also use the eval command instead of this. They are both valid, but this is more modern.


this["new"+i].name.text = memberInfo[i][0];

This adds the first item in our array which is the Name to our textbox called "name".


this["new"+i].interest.text = memberInfo[i][1];

This line of code will add our second item in our array which would be the interest text to our textbox called "interest".


this["new"+i].link = memberInfo[i][2];

This last line of code will define the variable link which we placed in our button so it points to the URL we specified in the last item of our array. Speaking of our button, let's look at the code for that next.


on (press) {
getURL(link, "_blank");
}

This is pretty straightforward. It's a simple getURL statement, with the URL being taken from the link variable you specified earlier. The "_blank" is the property that tells Flash to open a new window for your link.


I have provided the FLA for what you did above:

Download Source Files (ZIP)

If you have any questions, feel free to post on the kirupaForums: //www.kirupa.com/forum/

B.Rich
RoundedVision

 


page 2 of 2

 

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.