-
2d Array
I can't figure out how to use multidimensional arrays in as3, mainly a 2d one.
First I create an array
var 2dArray:Array=new Array();
then when I want to add stuff I go:
It doesn't work...
-
Originally Posted by shamimsaad03
It doesn't work...
That's because you have created a one-dimensional array using var 2dArray:Array = new Array();
To create a two or more dimensional array, you need to declare another array inside an array. So the code for a 2d array would be:
Code:
var 2dArray:Array = new Array(new Array());
2dArray[0][0] = 1;
-
Note that you must initialize each index in the outer array with a new array.
-
Bearded (M|G)od
Originally Posted by 5TonsOfFlax
Note that you must initialize each index in the outer array with a new array.
Meaning that:
Code:
var 2dArray:Array = new Array(new Array());
2dArray[0][0] = 1;
Will work, but:Won't.
You need to declare that each index is an Array first. So normally you'd be constructing something like this in a loop, and you can just take care of it within that like:
Code:
var a:Array = [];
for (var i:int=0; i<100; i++)
{
a[i] = [];
for (var j:int=0; j<100; j++) a[i][j] = i+":"+j;
}
Now you have a 100x100 matrix you can work with.
Also, variables cannot begin with a number. var 2dArray is invalid.
Last edited by MyFriendIsATaco; 01-24-2010 at 12:46 PM.
-
what if I wanted to push an object into 2dArray[2][1], how would I do that?
-
Array[2][1] is not itself an array, so you can't push anything into it. But you can set it to anything.
Code:
myArray[2][1] = new Object();
-
Originally Posted by 5TonsOfFlax
Array[2][1] is not itself an array, so you can't push anything into it. But you can set it to anything.
Code:
myArray[2][1] = new Object();
Can I retrieve it like:
var obj:Object = myArray[2][1];
?
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|