2D Array Map

Actionscript:
  1. var col:Array = [0x000000, 0xCCCCCC, 0xFF0000, 0xCCCC00, 0x000055, 0x00CCCC];
  2.  
  3. var map:Array = new Array();
  4. map[0] = [0,0,0,0,0,0,0,0,0,0];
  5. map[1] = [0,0,0,0,0,0,0,0,0,0];
  6. map[2] = [0,5,4,5,4,5,4,5,4,0];
  7. map[3] = [0,4,4,4,4,4,4,4,4,0];
  8. map[4] = [0,4,0,0,0,0,0,0,4,0];
  9. map[5] = [0,3,2,3,2,3,2,3,2,0];
  10. map[6] = [1,1,1,1,1,1,1,1,1,1];
  11. map[7] = [0,2,2,2,2,2,2,2,2,0];
  12. map[8] = [1,1,1,1,1,1,1,1,1,1];
  13.  
  14. for (var i:int = 0; i<map.length; i++){
  15.     for (var j:int = 0; j<map[i].length; j++){
  16.         graphics.beginFill(col[map[i][j]]);
  17.         graphics.drawRect(j * 10, i * 10, 10, 10);
  18.     }
  19. }

Use a 2D array to draw a map of colored rectangles. Lots of stuff you can do with this - like adding game tiles instead of drawing colored rects:

Actionscript:
  1. // add clip with multiple frames - each containing a tile graphic
  2. var tile:MovieClip = TileClip();
  3. tile.gotoAndStop(map[i][j]+1);
  4. addChild(tile);

This entry was posted in Graphics, arrays. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*