title: Programming Multiple Walls in Flash CS5/Actionscript 3 for Flash Game
body: <p>I am trying to program a Flash Game that utilizes all four directions to move the character. (i.e. Pressing up moves the character north, down - south, etc.) The character in the game does not actually move, but the background moves to simulate movement. I need to figure out how to go about programming the walls, though. I have code that recognizes collision, but what is the best way to do it? This is my code:</p>
  
  <pre><code>stage.addEventListener(Event.ENTER_FRAME,handleCollision);
  
  function handleCollision( e:Event ):void{
  
      if(hero.hitTestObject(wall))
         {
             wall.x-=5;
             Background.x-=5;
         }
  }
  </code></pre>
  
  <p>That recognizes the collision and stops the background and wall from moving, but that only works for when the character hits the wall from the right. Do I have to plug in this code to EVERY wall and tailor it to the situation? Or is there a more streamlined way of working with walls so that I can keep coding to a minimum?</p>
  
  <p>Update:
  I tried using Jason's idea of utilizing an array to keep track of the walls, but now I have another problem. This is my code so far:</p>
  
  <pre><code>stage.addEventListener(Event.ENTER_FRAME,onenter);
  </code></pre>
  
  <p>var walls:Array = [Northwall, Southwall, Eastwall, Westwall];</p>
  
  <p>function onenter(e:Event):void
  {</p>
  
  <pre><code>if(Key.isDown(Key.RIGHT))
  {
      Background.x-=5;
      hero.scaleX=1;
      for each(var wall:MovieClip in walls)
      {
          Northwall.x-=5;
          Southwall.x-=5;
          Eastwall.x-=5;
          Westwall.x-=5;
      }
      hero.gotoAndStop('walking sideways');
  }
  else if(Key.isDown(Key.LEFT))
  {
      Background.x+=5;
      hero.scaleX=-1;
      for each(var walls0:MovieClip in walls)
      {
          Northwall.x+=5;
          Southwall.x+=5;
          Eastwall.x+=5;
          Westwall.x+=5;
      }
      hero.gotoAndStop('walking sideways');
  }
  else if(Key.isDown(Key.DOWN))
  {
      Background.y-=5;
      for each(var walls1:MovieClip in walls)
      {
          Northwall.y-=5;
          Southwall.y-=5;
          Eastwall.y-=5;
          Westwall.y-=5;
      }
      hero.gotoAndStop('walking down');
  
  }
  else if(Key.isDown(Key.UP))
  {
      Background.y+=5;
      for each(var walls2:MovieClip in walls)
      {
          Northwall.y+=5;
          Southwall.y+=5;
          Eastwall.y+=5;
          Westwall.y+=5;
      }
      hero.gotoAndStop('walking up');
  }
  else
  {
      hero.gotoAndStop('still 1');
  }
  
  
  
  
   for each (var walls:MovieClip in walls) {  
     if (hero.hitTestObject(Northwall))
  {
      Background.y-=5;
  }
  else if (hero.hitTestObject(Southwall))
  {
      Background.y+=5;
  }
  else if (hero.hitTestObject(Westwall))
  {
      Background.x-=5;
  }
  else if (hero.hitTestObject(Eastwall))
  {
      Background.x+=5;
  }
  </code></pre>
  
  <p>The code compiles and everything runs, but the walls aren't registering. I think it has to do with the movement code in the if/else statements. I think the walls are moving with the character, instead of moving with the background, which means my character never reaches the walls to register the hittest. How can I code it so that the walls move with the background so I can actually test whether or not I can hit them?</p>
  
  <p>Update 2:
  I tried utilizing the code Jason provided in my own program, and it is working out quite nicely. The character moves and can hit walls, and most of the moving code functions as intended. However, only the Eastwall seems to function correctly. The Southwall seems to register from the side, and acts weird when I try to hit it from the North. Naturally, I would like to have it register from the North, rather than from the side, and have the same effect as when I hit the Eastwall. The Northwall and Westwall also don't seem to register at all. This is the code fully implemented:</p>
  
  <pre><code>var walls:Array = [Northwall, Southwall, Eastwall, Westwall];
  
  stage.addEventListener(KeyboardEvent.KEY_DOWN,keydownHandler);
  function keydownHandler(event:KeyboardEvent):void
  {
      switch (event.keyCode)
      {
          case Keyboard.RIGHT:
              moveWalls(-5, 0);
              Background.x-=5;
              hero.scaleX=1;
              hero.gotoAndStop('walking sideways');
              break;
          case Keyboard.LEFT:
              moveWalls(5, 0);
              Background.x+=5;
              hero.scaleX=-1;
              hero.gotoAndStop('walking sideways');
              break;
          case Keyboard.UP:
              moveWalls(0, -5);
              Background.y+=5;
              hero.gotoAndStop('walking up');
              break;
          case Keyboard.DOWN:
              moveWalls(0, 5);
              Background.y-=5;
              hero.gotoAndStop('walking down');
              break;
      }
  }
  
  function moveWalls(x:Number, y:Number):void
  {
      for each (var wall:DisplayObject in walls)
      {
          wall.x += x;
          wall.y += y;
      }
  }
  
  stage.addEventListener(Event.ENTER_FRAME,frameHandler);
  function frameHandler(event:Event):void
  {
      for each (var wall:DisplayObject in walls)
      {
          if (hero.hitTestObject(wall))
          {
              switch (wall)
              {
                  case Northwall:
                      moveWalls(0, -5);
                      Background.y-=5;
                      break;
                  case Southwall:
                      moveWalls(0, 5);
                      Background.y+=5;
                      break;
                  case Westwall:
                      moveWalls(-5, 0);
                      Background.x-=5;
                      break;
                  case Eastwall:
                      moveWalls(5, 0);
                      Background.x+=5;
                      break;
              }
          }
      }
  }
  </code></pre>
  
  <p>I think the problem might be in the hitTest. But I'm not sure. Also, if I hold down the button, the character meets resistance, but if held down long enough, the character ends up moving through the wall entirely. How can I stop the character from traveling through the wall?</p>