A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: 2d Array

  1. #1
    Senior Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    166

    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:

    2dArray[0][0] = 1;
    It doesn't work...

  2. #2
    Junior Member
    Join Date
    Jan 2010
    Posts
    8
    Quote Originally Posted by shamimsaad03 View Post
    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;

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Note that you must initialize each index in the outer array with a new array.

  4. #4
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Quote Originally Posted by 5TonsOfFlax View Post
    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:
    Code:
    2dArray[1][0] = 1;
    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.

  5. #5
    Senior Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    166
    what if I wanted to push an object into 2dArray[2][1], how would I do that?

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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();

  7. #7
    Senior Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    166
    Quote Originally Posted by 5TonsOfFlax View Post
    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];
    ?

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center