user1709407
user1709407

Reputation: 41

Flash as3 array and loop and function

I have an array:

var type:Array = [[[1,2,3], [1,2,3],[1,2,3]],
          [[1,2,3], [1,2,3],[1,2,3]]];

Then I loop it to call the a function:

for(var i:int = 0;i<type.length;i++) {
    addGrid(type[0][i]);
}

The function that I'm calling is:

public function addGrid(col,row:int, type:Array) {
    var newGird:GridE = new GirdE();
    newGird.col = col;
    newGird.row = row;
    newGird.type = type;
}

Hope it clear what i need. My Gird can be a big as the array is for the Array sample in here the Gird would be 3(Columns)x2(Rows)

Upvotes: 0

Views: 540

Answers (1)

Jason Sturges
Jason Sturges

Reputation: 15955

ActionScript 3 multidimensional arrays may be referenced using multiple array indexes by looping your row and column.

Per your array structure, you first define rows, then columns.

This makes a lookup for cell value:

grid[row][col]

Iterating all elements could be implemented as:

private var grid:Array = [[[ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ]],
                          [[ 1, 2, 3 ], [ 1, 2, 3 ], [ 1, 2, 3 ]]];

public function loop()
{
    // for every row...
    for (var row:uint = 0; row < grid.length; row++)
    {
        // for every column...
        for (var col:uint = 0; col < grid[row].length; col++)
        {
            // your value of "1, 2, 3" in that cell can be referenced as:
            //    grid[row][col][0]  = 1
            //    grid[row][col][1]  = 2
            //    grid[row][col][2]  = 3

            // to pass row, col, and the value array to addGrid function:
            addGrid(row, col, grid[row][col]);
        }
    }
}

public function addGrid(row:int, col:int, value:Array):void
{
    /* ... */
}

Upvotes: 2

Related Questions