nvoss
nvoss

Reputation: 13

Multi-dimensional Arrays in AS3

I am currently playing around with flex, I have C++ background, so I am not used to AS3. The problem is in the main *.mxml file I have fx:script block and I try to define a multidimensional array like that:

public var Board:Array = new Array(25);

I use a function to initialize the 2d-array:

public function initBoard():void {
    var i:int;
    var j:int;
    for (i = 0; i < 25; i++) {
        Board[i] = new Array(40);
        for (j = 0; i < 40; j++) {
             Board[i][j] = 0;
        }
    }
}

This function gets called later on in the main loop to init and reset the "board" why doesn't it work. The only difference to the AS3 documentation is that it gets done in a function. Is there a scope problem?

Thanking you in anticipation, Niklas Voss

P.S. I hope someone can tell me why it doesn't work and how to do it right...

Upvotes: 1

Views: 3506

Answers (2)

Marty
Marty

Reputation: 39466

You don't need to define an array length in AS3 - I just use the [] operator for creating a new array. Also you used i where j was needed in the innermost for loop.

function initBoard():Array
{
    var board:Array = [];

    var i:int = 0;
    var j:int;

    for(i; i<25; i++)
    {
        board[i] = [];

        j = 0;
        for(j; j<40; j++)
        {
            board[i][j] = 0;
        }
    }

    return board;
}


trace(initBoard());

Upvotes: 3

phwd
phwd

Reputation: 19995

You have i where there should be j.

for (j = 0; i < 40; j++) {

This should solve your problems.

for (j = 0; j < 40; j++) {

Upvotes: 5

Related Questions