A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: [RESOLVED] how to create multidimensinal arrays? AS3

  1. #1
    Member
    Join Date
    Jun 2008
    Posts
    34

    resolved [RESOLVED] how to create multidimensinal arrays? AS3

    var test:Array = new Array();

    test[1][55][10][38] = "some value"; //is not working
    test[2] = "value"; //is working

    Though I think this code will work for retrieving data from multidimensional array:
    trace(test[1][55][10][38]);

    I just do not know how to make one.

  2. #2
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    Here you go:

    var ar:Array=[1,2,[3,4,5]];
    trace("Array: ",ar,"\nArray element 0:", ar[0], "\nArray element 1:", ar[1], "\nArray Element 2:", ar[2], "\nFirst Element of Array Element 2: ", ar[2][1]);

    EDIT: Oops, I think I missed what you wanted.
    First, you can do like ar[2]=[3,4,5];

    You can also do something like

    var ar=new Array(6);
    for (var i=0; i<ar.length; i++){
    ar[i]=new Array(6);
    }

    Then, you can access it like ar[2][3]=5;
    Last edited by sphoenixee; 06-22-2008 at 05:31 PM.

  3. #3
    Member
    Join Date
    Jun 2008
    Posts
    34
    Ok, so I have to make 4 loops if I need 4-dimension array?
    And CPU has to loop through all of it just to do simple
    var arr dim(100,100,1000,5);


    What a lovely, lovely language

  4. #4
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    Maybe there is a better way. You bring up a very good point.

    Unfortunately that better way is not mentioned in Adobe's documentation/techsupport (or at least in the 2 pieces that I read).

    This is a bit depressing. Hopefully someone knows of better way. Then again, I do not think Flash was meant to deal with arrays that big.

    By the way, why do you need such a big array?

  5. #5
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Not really. You just need to define each level as an array, that's the only problem.

    If you call: test[1][55][10][38] = "some value";

    What is test[1][55] ? What if it was a Number? Or a String?

    Each level needs to be declared as an array first.

    test = new Array();
    test[1] = new Array();
    test[1][55] = new Array();
    test[1][55][10] = new Array();
    test[1][55][10][38] = "some value";

  6. #6
    Member
    Join Date
    Jun 2008
    Location
    US
    Posts
    84
    If you do that though, what if you want to access test[1][54][3][39]? (or anything else but test[1][55][10][38]? It does not work... I think that is what OP wanted (namely being able to access everything in the array, not just a few elements)...but I may also be wrong on that.

  7. #7
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    You are right. The other elements are inaccessible.

    It'd help if I knew the purpose of needing such a large dimensional array. I've never seen the use of anything more than [x][y][z].

  8. #8
    Member
    Join Date
    Jun 2008
    Posts
    34
    I am making a tile based game... And I figure that Array is fastest way to deal with huge amount of data. So my thoughts was like this:

    Arr[i][j][k][l]

    i =number of "objects"
    j*k = defines space like x*y coordinates
    l = value/bmp that gets drown on screen but l will change its value sometimes

    What is test[1][55] ? What if it was a Number? Or a String?
    That was my whole point:
    if I create test[1][55][10][38] = "some value";

    trace(test[1][55]) is meaningless! You can not access 4 dim array by 2dim reference!

    So, how do you create [x][y][z] array?

  9. #9
    Member
    Join Date
    Jun 2008
    Posts
    34

    Unhappy

    I just tested what sphoenixee suggested. It is horribly slow at creating but it works.

    var niz:Array = new Array();

    for(var i:uint = 0; i < 10000; i++)
    {

    niz[i] = new Array();
    for(var j=0;j<100;j++)
    {
    niz[i][j] = new Array();
    for(var k:int = 0; k<10;k++)
    niz[i][j][k] = k;
    }
    }
    That makes niz:Array of 10000*100*10. And I can add values like this: niz[x][y][z] = "value";

    We solved it. Kind of... But it really sucks

  10. #10
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    This is crazy overkill...set up a simple x by y two-dimension array.

    test[x].length will give you the number of columns you have, test[x][y].length is the number of rows in a given column (there's nothing in the code that says all columns will be the same length...)

    For the value to draw on screen...either store that as a property of the sprite/mc/tile or else use a dictionary...ie:

    var terrain_by_spriteictionary = new Dictionary();
    terrain_by_sprite[test[x][y]] = "dirt";

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Also, lower level languages such as C simply reserve a linear block of memory equivalent to the product of all dimensions. That is to say a 5x3 2 dimensional array is equivalent to a 15 element one dimensional array. You can manually determine the 1 dimensional equivalent address by multiplying the size of each dimension. something[5][4][3] in a 3 dimensional(10x11x12) array could be something2[5*11*12 + 4*12 + 3].

    I think. I'm not 100% sure on that math at the end.

  12. #12
    Member
    Join Date
    Jun 2008
    Posts
    34
    Quote Originally Posted by neznein9
    test[x].length will give you the number of columns you have, test[x][y].length is the number of rows in a given column (there's nothing in the code that says all columns will be the same length...)
    Ok, but how to make var test:Array of 100*1000 elements?


    Quote Originally Posted by neznein9
    For the value to draw on screen...either store that as a property of the sprite/mc/tile or else use a dictionary...ie:

    var terrain_by_spriteictionary = new Dictionary();
    terrain_by_sprite[test[x][y]] = "dirt";
    It will be sprite

    @5TonsOfFlax:
    Yes! I googled your post earlier. It could be done that way and your math is right.
    something2[5*11*12 + 4*12 + 3].

    array.length = length*height*width; //basically a stream of data

    array[ (x) + (length * y) + (length * height * z)];
    where x has to be x < width

  13. #13
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Well, you could get away with a single loop to build the structure:

    PHP Code:
    const cols:int 100;
    const 
    rows:int 1000;

    while(
    rows 0){
        
    rows--;
        
    test.push(new Array(cols));    

    But you'd still need a double loop to touch each cell to populate:

    PHP Code:
    const cols:int 100;
    const 
    rows:int 1000;

    while(
    rows 0){
        
    rows--;
        while(
    cols 0){
            
    cols--;
            
    test[rows].push(new Sprite());
        }    


  14. #14
    Member
    Join Date
    Jun 2008
    Posts
    34
    Thanks neznein9, that's similar to post #9.


    @5TonsOfFlax:

    I tried timing single array and math approach. It's a little slower that [x][y] access.
    If you have 4D array, math gets really hard to read because you have to add and multiply all that dimensions.

    Oh, I realized that I was multiplying IN the loop!!! length * height could be hard coded as a number or one variable!!!
    And, there's usage or int. It's fast for + - but slow for *.
    Maybe number will be faster!

  15. #15
    Member
    Join Date
    Jun 2008
    Posts
    34
    As expected putting premultiplied variable was a success! 30ms gain
    int was faster than Number by few milliseconds.

    EDIT:

    I have to correct myself. 2D and 3D array access is FASTER by 5TonsOfFlax math!

    680ms for array[x][y] vs. 640ms for array[math***) on average per million loops.
    Last edited by DevilMayCry; 06-22-2008 at 09:42 PM.

  16. #16
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Glad to hear that it works out for you. I'd actually probably make the sacrifice and create a real multidimensional array in my code unless it was extremely performance bound. I just value logical structure and readability more than a few milliseconds.

    One notable exception is the ByteArray returned by BitmapData.getPixels, which is already a 1d array representing the 2d image data.

  17. #17
    Knows where you live
    Join Date
    Oct 2004
    Posts
    944
    Quote Originally Posted by 5TonsOfFlax
    Glad to hear that it works out for you. I'd actually probably make the sacrifice and create a real multidimensional array in my code unless it was extremely performance bound. I just value logical structure and readability more than a few milliseconds.
    I'll second this. In my experience with testing 2d and 3d arrays, while constructing a multidimensional array takes longer using it is faster.
    As well, dropping in an 80 character operation for every lookup is a pain in the ass to maintain and code. Overlaying a function call to do it for you makes things easier but completely destroys performance.
    The greatest pleasure in life is doing what people say you cannot do.
    - Walter Bagehot
    The height of cleverness is to be able to conceal it.
    - Francois de La Rochefoucauld

  18. #18
    Member
    Join Date
    Jun 2008
    Posts
    34
    Thank you all for helping. I'll use 2.5D array Only AS3 allows that

    Yet I still can not believe that I have to use loops to create arrays. Goes with a job I guess...

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