A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: array manipulation

  1. #1
    Senior Member charlie_says's Avatar
    Join Date
    Feb 2003
    Location
    UK
    Posts
    508

    array manipulation

    i have been designing screens for my tile based game, and having got a little tired with typing in lots of numbers to an array, I made myself a little tile editor.
    then I hit a little snag, which I'm sure someone must have come across before. Some of my screens are essentially the same altho flipped across an axis- now I could just remake them, it won't take too long, ut it seemd to me to be a good solution to create a flip button in my tile editor.
    Can someone tell me good way of flipping a 2 dimensional array, both horizontally AND vertically (altho as separate operations?)
    Thanks.

  2. #2
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    This code assumes that each horizontal array is of the same length.
    vertically;
    Code:
    var t, bEntry, eEntry;
    var begin = 0;
    var end = myArray.length - 1;
    var l = myArray[0].length;
    do {
     bEntry = myArray[begin];
     eEntry = myArray[end];
     for(i=0;i<l;i++) {
      t = bEntry[i];
      bEntry[i] = eEntry[i];
      eEntry[i] = t;
     }
    } while(--end > ++begin);
    horizontally;
    Code:
    var t;
    var begin = 0;
    var end = myArray[0].length - 1;
    var l = myArray.length;
    do {
     for(i=0;i<l;i++) {
      t = myArray[i][begin];
      myArray[i][begin] = myArray[i][end];
      myArray[i][end] = t;
     }
    } while(--end > ++begin);
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

  3. #3
    Senior Member charlie_says's Avatar
    Join Date
    Feb 2003
    Location
    UK
    Posts
    508
    sweet...
    you've probably saved me about a million hours work there.
    thankyou!
    Last edited by charlie_says; 12-16-2004 at 05:54 AM.

  4. #4
    Senior Member
    Join Date
    Jul 2003
    Posts
    138
    This might come in handy, too. It will rotate a 2-dimensional array 90 degrees either clockwise or anti-clockwise. You just need to pass it an array (first parameter) and a direction (second parameter; 1 for clockwise, and any othe r value for anti-clockwise). It returns the rotated array.

    Though the two dimensions can be of different lengths, the array cannot be jagged.

    code:

    function RotateArray(myArray, rotateDirection) {

    // Create temporary array
    tempArray = new Array();
    for (a = 0; a < myArray[0].length; a++) {
    tempArray[a] = new Array();
    }

    // Create variables to store current position in temporary array
    ny = 0;
    nx = 0;
    if (rotateDirection == 1) {

    // Perform clockwise rotation
    for (x = 0; x < myArray[0].length; x++) {
    for (y = myArray.length-1; y >= 0; y--) {
    tempArray[ny][nx] = myArray[y][x];
    nx++;
    }
    ny++;
    nx = 0;
    }

    } else {

    // Perform anti-clockwise rotation
    for (x = myArray[0].length-1; x >= 0; x--) {
    for (y = 0; y < myArray.length; y++) {
    tempArray[ny][nx] = myArray[y][x];
    nx++;
    }
    ny++;
    nx = 0;
    }
    }

    return tempArray;
    }


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