PDA

View Full Version : Two Dimensional Arrays



Relain
July 12th, 2004, 09:30 AM
Well, first of all Hello Everyone.

I've been trying to use ActionScript to create an isometric pseudo3d terrain generator thing, using a dot movie clip.

I come from a C programming background and i thought it'd be pretty easy to just attach a load of movie clips to the stage and put them all (well references to them) into a 2d array so i could call back to them with ease for the moving and the jiggery pokery.

This is what i think sets up a 2d array called point_set, pretty strange syntax a mon avis...

point_set = new Array;
point_set = [[],[]];

Then i attach the movie clips using some nested for loops:

vertex = attachMovie("mcPoint", "n"+total, total);
point_set[j][i] = vertex;

Then when i try and actually reference them, in this fashion:

onEnterFrame = function()
{
for (j=0; j<height; j++)
{
for (i=0; i<width; i++)
{
node = point_set[j][i];
// rest of the function...

but i only get a few of the points in the onEnterFrame. Viz:

www.relain.co.uk/stuff/flash/landscape.fla (http://www.relain.co.uk/stuff/flash/landscape.fla)

and actually at:

www.relain.co.uk/stuff/flash/landscape.swf (http://www.relain.co.uk/stuff/flash/landscape.swf)

[it is actually an isometric grid, well on a Pi/6 angle but its not set up yet]

Well thanks a lot for any help folks

pom
July 12th, 2004, 03:51 PM
Unexpected file format. Can you save as Flash MX?

senocular
July 12th, 2004, 04:03 PM
some of the fla code was a little different... you were using point_set[[j], [i]] in some places which is incorrect.

In Flash, a 2D array is basically just a basic array of arrays (nothing fundamentally unique as a structure). So your first dimension in the array, your j collection of indices is the main array whos elements are all arrays to hold your values. This means that for each iteration of j in your 2d array creation/assignment, you need to create a new array at that index. From there, you can then add values to that interior array's indices making up the elements of the full 2d array.


point_set = new Array(); // all you need to start
for (j=0; j<height; j++){
point_set[j] = new Array(); // adding your second dimension - needed for each j
for (i=0; i<width; i++){
point_set[j][i] = value; // point_set[j] is now an array and an i element can be accessed via array access brackets []
}
}