Flash Trek: The Unofficial Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

How do make a Multi-Dimensional Array in Flash

2 posters

Go down

How do make a Multi-Dimensional Array in Flash Empty How do make a Multi-Dimensional Array in Flash

Post by stewi0001 2008-12-03, 14:12

I know this is the "Ask Darkwing" section but hey I got to abuse my power sometimes Razz Anyways I take no credit for this I only ran into it on another forum:

Here a Tutorial about Multi-Dimensional Array!
Author: Lisa Kushins


quote:
--------------------------------------------------------------------------------

Actionscript, like Javascript, doesn't "officially" support multidimensional arrays. However, both languages DO allow you to simulate multidimensional arrays by letting you create "arrays of objects" and those objects themselves can be Arrays.
Today I'm going to show you how to create your own objects and put them in an array.
1. Create a constructor for an object we'll call friend.

This object will later be used to fill each element in our array.

ActionScript:--------------------------------------------------------------------------------

Code:
// Object constructor
function friend(name, phone, birthday, address) {
        this.name = name;
        this.phone = phone;
        this.birthday = birthday;
        this.address = address;
}
--------------------------------------------------------------------------------

Looks complex, right? But all the above function does is create an object called friend that has four properties: name, phone, birthday and address. When we create our array, each element in the array will contain a friend object with these properties.

2. Now create the main array that will hold all our friend objects.
(This is where the multidimensional part comes in!)

ActionScript:--------------------------------------------------------------------------------

Code:
// Create our main array
var lisasFriends = new Array();

// Populate the lisasFriends array with friends objects
lisasFriends[0] = new friend("Tim", "212-555-1212", "March 30, 1973", "30 Tim Lane");
lisasFriends[1] = new friend("David", "718-555-1234", "December 12, 1971", "67-98 Winter Road");
lisasFriends[2] = new friend("Sally", "201-555-1908", "April 1, 1969", "4 Rice Court");

--------------------------------------------------------------------------------

You've just created an array called lisasFriends, and filled it with 3 elements. The line:

ActionScript:--------------------------------------------------------------------------------
Code:
lisasFriends[0] = new friend("Tim", "212-555-1212", "March 30, 1973", "30 Tim Lane");
--------------------------------------------------------------------------------

creates the first element in the array (arrays in Flash start with a 0 index) and sets the values for Tim's name, phone number, birthday and address. Having all this information in an array does nothing for us though, unless we know how to access it. So read on. :-)

3. It's time to start reading information from our lisasFriends array.

ActionScript:--------------------------------------------------------------------------------
Code:
// Use the trace function to print out a value in our array.
trace("lisasFriends[0].name = " + lisasFriends[0].name);

--------------------------------------------------------------------------------

The value in lisasFriends[0].name is Tim, the name in the 0th element in our array. To print out ALL the names in your array, you can use something like this:

ActionScript:--------------------------------------------------------------------------------
Code:
for (i=0; i<lisasFriends.length; i++) {
        trace("lisasFriends[" + i + "].name = " + lisasFriends[i].name);
}
--------------------------------------------------------------------------------

The above trace statement will print out the following:

ActionScript:--------------------------------------------------------------------------------
Code:
lisasFriends[0].name = Tim
lisasFriends[1].name = David
lisasFriends[2].name = Sally
--------------------------------------------------------------------------------

Pretty cool, right? But I know what you're thinking - now that you know how to create arrays of custom objects, you want to know how to create arrays of arrays so you can start using code like: someVariable = myArray[3][2]; So let's get started!

4. Arrays of arrays of arrays...

Let's make a new lisasFriends array, called lisasFriends2, that is an array of array objects.

ActionScript:--------------------------------------------------------------------------------
Code:
// Create our new main array
var lisasFriends2 = new Array();

// Populate the lisasFriends2 array with arrays
lisasFriends2[0] = new Array("Betty", "317-090-8765", "January 22, 1972", "1 Jefferson Lane");
lisasFriends2[1] = new Array("Wanda", "317-789-8982", "August 9, 1967", "30 River Court");
--------------------------------------------------------------------------------

Now, if you want to print out Betty's phone number, you could use the following statement:

ActionScript:--------------------------------------------------------------------------------
Code:
trace("Betty's phone number = " + lisasFriends2[0][1]);
--------------------------------------------------------------------------------

In the above line of code, the [0] represents the first element in our main lisasFriends2 array, which is Betty's information. The [1] represents the second (don't forget - arrays in flash start with a 0 index! :-) ) element in Betty's array of information, which is her phone number.

If you want to print out everything in your multidimensional array, you have to use a nested for loop. All that really means is that we use a for loop inside another for loop, like in the code below:

ActionScript:--------------------------------------------------------------------------------
Code:
for (i=0; i<lisasFriends2.length; i++) {
        for (j=0; j<4; j++) {
                trace("lisasFriends2[" + i + "][" + j + "] = " + lisasFriends2[i][j]);
        }
}
--------------------------------------------------------------------------------

That's it! You've just created a multidimensional array! You can see all the code used in this tutorial with comments, in the actions layer of the fifth frame in this flash movie. Happy coding! :-)

--------------------------------------------------------------------------------
stewi0001
stewi0001
Lieutenant
Lieutenant

Male Number of posts : 946
Age : 38
Location : Pittsburgh, PA
Registration date : 2007-10-17

http://mormon.org/mormonorg/eng/

Back to top Go down

How do make a Multi-Dimensional Array in Flash Empty Re: How do make a Multi-Dimensional Array in Flash

Post by me naam is m 2008-12-03, 14:40

heck, that does explain simple arrays too!

*thinks*

YES!

now if i could find where vex left that darn piece of code...
me naam is m
me naam is m
Commander
Commander

Male Number of posts : 9981
Registration date : 2007-12-26

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum