topical media & game development

talk show tell print

actionscript-misc-TileMap.ax

actionscript-misc-TileMap.ax [swf] flex


  package {
    import flash.errors.IllegalOperationError;
  
    // Represents a rectangular grid of tile IDs
    public class @ax-actionscript-misc-TileMap {
      private var map:Array;    // A 2-d array of tile IDs
      private var numCols:int;  // The number of columns in the grid
      private var numRows:int;  // The number of rows in the grid
      
      // Constructor
      public function @ax-actionscript-misc-TileMap (numCols:int, numRows:int) {
        setSize(numCols, numRows);
      }
  
      // Sets the size of the map and removes all existing tile IDs
      public function setSize (numCols:int, numRows:int):void {
        this.numCols = numCols;
        this.numRows = numRows;
  
        map = new Array();
        for (var i:int = 0; i < numRows; i++) {
          map[i] = new Array();
        }
      }
  
      // Assigns a tile ID to a specified position in the map
      public function setTile (col:int, row:int, id:int):void {
        if (col >= numCols || row >= numRows) {
          throw new IllegalOperationError("Specified tile location "
                                          + "exceeds map boundaries");
        }
        map[row][col] = id;
      }
  
      // Returns the tile ID for a specified position in the map
      public function getTile (col:int, row:int):int {
        return map[row][col];
      }
      
      // Returns the number of columns in the map
      public function getNumCols ():int {
        return numCols;
      }
      
      // Returns the number of rows in the map
      public function getNumRows ():int {
        return numRows;
      }
  
      // Generates a random tile ID for each position in the map.
      // Random IDs fall in the range 0 to numTiles-1.
      public function randomize (numTiles:int):void {
        for (var i:int = 0; i < getNumRows(); i++) {
          for (var j:int = 0; j < getNumCols(); j++) {
            setTile(j, i, Math.floor(Math.random()*numTiles));
          }
        }
      }
    } 
  }
  


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.