title: Game tile movement in 2d map
body: <p>What's the best way to do movement in a 2D square grid system?  I have this something that works but it seems wrong/ugly (see below).</p>
  
  <pre><code>x x x x x x x
  x x x x x x x
  x x x O x x x
  x x x U x x x
  x x x x x x x
  x x x x x x x
  x x x x x x x
  </code></pre>
  
  <p>For example, U is the unit I want to move, and O is an impassable object like another unit or a mountain.  If U can move 3 tiles, I want the moveable area (M) to look like this.</p>
  
  <pre><code>x x x x x x x
  x x M x M x x
  x M M O M M x
  M M M U M M M
  x x M M M M x
  x x M M M x x
  x x x M x x x
  </code></pre>
  
  <p>Here's my code:</p>
  
  <pre><code>public function possibleMoves(range:uint, cords:Array):void {
  var X:uint = cords[0];
  var Y:uint = cords[1];
  
  if (range &gt; 0) {
      try {
          theGrid[X + 1][Y].moveable = true;
          if (theGrid[X + 1][Y].getOccupied == false) {
              possibleMoves(range - 1, [X + 1, Y], flag, mtype);
          }
      }   catch (err:Error) { }
  
      try {
          theGrid[X - 1][Y].moveable = true;
          if (theGrid[X - 1][Y].getOccupied == false) {
              possibleMoves(range - 1, [X - 1, Y], flag, mtype);
          }
      }   catch (err:Error) { }
  
      try {
          theGrid[X][Y + 1].moveable = true;
          if (theGrid[X][Y + 1].getOccupied == false) {
              possibleMoves(range - 1, [X, Y + 1], flag, mtype);
          }
      }   catch (err:Error) { }
  
      try {
          theGrid[X][Y - 1].moveable = true;
          if (theGrid[X][Y - 1].getOccupied == false) {
              possibleMoves(range - 1, [X, Y - 1], flag, mtype);
          }
      }   catch (err:Error) { }
  }
  </code></pre>