4

I was wondering if there was a better way to create a large 2D array and populate it with a single item with AS3? This is a quick example what I'm currently doing:

private var array:Array = [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
                           [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
                           [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
                           [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
                           [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]];

But there has to be a more functional way! Thanks in advance.

4 Answers 4

12

Can't you just use a 'traditional' loop to fill it? Something as simple as

var numCols:uint = 10,
    numRows:uint = 10,
    fillValue:uint = 1,
    array:Array = new Array(),
    i:uint,
    j: uint;

for (i = 0; i < numRows; i++) {
  array.push(new Array());
  for (j = 0; j < numCols; j++) {
    array[i].push(fillValue);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

3

I always use a single dimentional array and write my own get/set functions to work out the location in the array for the (x,y) point:

i.e, Getting an element:

return array[x+(y*_width)];

To reset the array or set it (once it's been allocated)

for(var i:uint=0;i<array.length;i++)
    array[i] = 1;

Main points are:

  • You only need to allocate a single array
  • You can reset or set or copy elements really fast
  • I assume flash uses "less" memory or there is less overhead overall as only a single array exists

One down side is making sure you accessor and setter functions do range checking as your results may "work" but not be accurate. (I.e. if x is greater than width but still within the bounds of the array)

I "grew up" on C (the language :-p) so this just always seems the logical way of doing things; Allocate a block of memory and divide it up how you want.

Comments

1

correct answer provided by kkyy ... although i'd say classically, you should rather use i < numCols and j < numRows, so access is array[column][row] ...

also, for more performance:

var numCols:uint = 10,
    numRows:uint = 10,
    fillValue:uint = 1,
    array:Array = new Array(),
    columnProto:Array = new Array(),
    i:uint;

for (i = 0; i < numRows; i++)
    columnProto.push(fillValue);
for (i = 0; i < numCols; i++)
    array.push(columnProto.slice());

results in much less instructions ... but you'll only notice the difference when numCols * numRows is considerably big ...

2 Comments

+1 for the slice technique. But aren't 2D arrays be accessed the other way, i.e., array[row][column]? The 2D array in the question contains 5 rows and 22 columns, right?
well, actually, this really depends. this arrays seems to represent 2d data (keen assumption, but it looks like the initial level you see all the time in small games), which you access through coordinates, where classically x goes first, and then y, as in parameters for about any 2d functions. the opposite often leads to mistakes. the literal strongly suggests something else, but that is due to the fact arrays work. now if the array actually represented a data set, then array[index][field], i.e. array[row][column] would be more appropriate. in the end, it's a matter of taste ... :)
0
Creating a 2D Array

var multiDimensionalArray:Array = new Array();
var boolArray:Array;
var MAX_ROWS = 5;
var MAX_COLS = 5;

//initalize the arrays
for (var row = 0; row <= MAX_ROWS; row++)
{
    `boolArray` = new Array();
    enter code here
    for (var col = 0; col <= MAX_COLS; col++)
        boolArray.push(false);
    }

    multiDimensionalArray.push(boolArray);
}

//now we can set the values of the array as usual
for (var row = 0; row <= MAX_ROWS; row++)
{
    for (var col = 0; col <= MAX_COLS; col++)
        boolArray[row][col] = true;
        trace('boolArray ' + row + ',' + col + ' = ' + boolArray[row][col]);
    }
}

I hope this will b usefull for u.,...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.