PDA

View Full Version : Creating a 2D array?



nutcase88
October 8th, 2007, 01:23 AM
Hi im trying to create a 2d array in flash and the fact that im a C++ programmer isnt helping me so im a little lost here.

Ive searched the forums and found something like this


var urls:Array=new Array();
for(i=0;i<3;i++)
{
urls[i] = new Array();
for(k=0;k<3;k++)
{
trace(urls[i][k]);
}
}




However when i put it in the trace returns undefined.


I am looking to create something like a 14 x 14 2D grid where all the variables inside are 0 at the beginning.

I did something like so



myArray = newArray();
for(int i =0; i< 14;i++){
for(int k =0; k< 14;k++){
myArray[i][k] =0;
}
}


But unfortunately it doesnt work.

Please advise, thanks in adv!

ptobias
October 8th, 2007, 01:53 AM
The code you found in the forums returns undefined because the values in the array were not initialized. The default value for an array's element is undefined.

Here code for a 14x14 Grid with all values initialized to 0:


var arySize:Number = 14;
var myArray:Array = new Array( arySize );

for( var i:Number = 0; i < arySize; i++ )
{
myArray[i] = new Array( arySize );
for( var k:Number = 0; k < arySize; k++ )
{
myArray[i][k] = 0;
}
}

nutcase88
October 8th, 2007, 02:41 AM
Thanks for the help, appreciate it!