Code in frame 1 layer 1


//tile-based world

//tbw.fla

//By Andy Harris, Dummies Guide to Game Programming in Flash



rowOffset = 25;

colOffset = 25;

numRows = 15;

numCols = 20;





tileName = new Array("town", "grass", "mountain", "trees", "water");

board = new Array (

  new Array (2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2),

  new Array (2, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2),

  new Array (2, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2),

  new Array (2, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 1, 1, 2),

  new Array (2, 1, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 2),

  new Array (2, 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 1, 2),

  new Array (2, 1, 1, 1, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1, 2),

  new Array (2, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 2),

  new Array (2, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 3, 1, 2),

  new Array (2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2),

  new Array (2, 1, 0, 0, 0, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2),

  new Array (2, 1, 0, 0, 0, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2),

  new Array (2, 0, 0, 0, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 2),

  new Array (2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 2),

  new Array (2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2)

); 



init();

function init(){

  for (col = 0; col < numCols; col++){

    for (row = 0; row < numRows; row++){

      _root.attachMovie("tile", "tile_" + col + "_" + row, 100 * col + row);

      theTile = eval("tile_" + col + "_" + row);

      theTile._x = (col * theTile._width) + colOffset;

      theTile._y = (row * theTile._height) + rowOffset;

      //store row and column information in instance

      theTile.row = row;

      theTile.col = col;



      //extract tileType from two-dimension array

      tileType = board[row][col];

      

      theTile.gotoAndStop(tileName[tileType]);

      //let tile change state.

      theTile.onRelease = function(){

        //get current state from board

        currentState = board[this.row][this.col];

        //advance state by one

        currentState++;

        if (currentState > 4){

          currentState = 0;

        } // end if

        //store state in board and show new state.

        board[this.row][this.col] = currentState;

        this.gotoAndStop(tileName[currentState]);

      } // end onRelease

    } // end row for

  } // end col for



  //create player

  _root.attachMovie("player", "player", 99999);

  player._xscale = 30;

  player._yscale = 30;

  player.onEnterFrame = function(){

    //check keys

    if (Key.isDown(Key.UP)){

      player._y -= 25;

    } // end if



    if (Key.isDown(Key.DOWN)){

      player._y += 25;

    } // end if



    if (Key.isDown(Key.LEFT)){

      player._x -= 25;

    } // end if



    if (Key.isDown(Key.RIGHT)){

      player._x += 25;

    } // end if

  } // end player movement

} // end init