开发者

Creating a large 2D array and populating it in AS3

I was wondering if there was a better way to cre开发者_JAVA百科ate 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.


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);
  }
}


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.


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜