views:

393

answers:

3

Hi, 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.

+7  A: 

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);
  }
}
kkyy
+1  A: 

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 ...

back2dos
+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?
Amarghosh
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 ... :)
back2dos
+1  A: 

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.

widgisoft
This is a good idea, thanks for the tips!
James James