|
|
4 RSS feeds listed in the Graphic Design category! |
![]() | Go one level up |
![]() | http://feeds.feedburner.com/Choruslinea1qms-fre... | ![]() |
![]() | choruslinea1qms, ... |
![]() | http://202.83.33.38 | ![]() |
![]() | Ammadey, ... |
![]() | http://elburlador.blogspot.com/ | ![]() |
![]() | fdiazmas, ... |
![]() | http://www.blog.0tutor.com/rss.aspx | ![]() |
![]() | jonespr, ... |
Not so long ago I made a tutorial on how to make and use simple one dimensional arrays with flash actionscript 3.0, now we will go a bit a further and develop our skill to create multidimensional arrays.
Not that this is more complex then the first, one dimensional array, but its worth while digging into, so if you think your ready, lets get started.
There are different kinds of multidimensional arrays, the one we will be dealing with now is using a technique to nest arrays into each other, another way, which we will get into in another article is to create data of the data type object. I won''t get into this right now, because that''s another story for later.
First we will define a simple array with two other arrays nested inside it, then I''ll show you how to call the data from it.
var myArray:Array = new Array(new Array("one","two"), new Array("three", "four"));
trace(myArray);
As you see if you test this example, you get an output trace with all the items one two three and four, this looks simple, but this time I promise you, it will get a bit more complicated.
Now we only want to extract some of the data, lets say two and four.
var myArray:Array = new Array(new Array("one","two"), new Array("three", "four"));
trace(myArray[0][1]);
trace(myArray[1][1]);
Lets take a look at how we extract the item "two", first we call the myArray, tells it to get into the first array (0 indexed) and take its second item "two" (has the index 1).
The second trace does the same, first we call the myArray, tells it to get into the second array (indexed 1) and again we want to extract the second item "four" (has index 1).
Okay that was simple, but think if we had 4 or even 10 arrays nested, then it will demand some thinking..
And now that we have learned the basics of how to construct a multidimensional array, what should we do with it?
Well lets say you are building a simple board game, it could be chess, it could be something else, but what board games got in common is they usually are build up by a grid in some way, chess got white and black squares. What we can do then is to construct that grid with a multi dimensional array. I will show you an example and try to explain how its made up.
var gridSize:int = 2;
var mainArr:Array = new Array(gridSize);
var i:int;
var j:int;
for (i = 0; i < gridSize; i++) {
mainArr[i] = new Array(gridSize);
for (j = 0; j < gridSize; j++) {
mainArr[i][j] = "[" + i + "][" + j + "]";
}
}
trace(mainArr);
This might seem a bit confusing, but try to test the code, you should get something like this. [0][0],[0][1],[1][0],[1][1] this is a two dimensional array, shown as a grid, you can refer to each column or row, as you can see now you can add to a three dimensional array or four to make big grids.
First we create a variable called gridsize to tell how many rows we want, this will be 2, then we set a main array object to hold it all.
We declare two variables i and j one for each column, we just add to this if we want more columns.
Finally we loop through the site (rows) for both j and i columns and set the values of each items in the arrays.
In the next array tutorial we will create array objects containing objects with seperate properties and values, like I explained in the description above.
![]() | Date: Thu, 6 Nov 2008 08:23:00 EST |
|
|