tags:

views:

37

answers:

2

I have a MovieClip that can move up, down, left and right. If the MovieClip hits a wall, the MC bounces, and should not move past the wall--but if you continue to press either your up, down, left or right key, and don't let go--the MovieClip will go past the wall. I'm trying to figure out a way, to stop that from happening. link of movement

    // function hitWall --------------------------------------------------------------
    function hitWall(event:Event):void {
        if (box.hitTestObject(wall)) {
                box.y+=6;
            } else if (box.hitTestObject(wall2)) {
                box.y-=6;

            } else if (box.hitTestObject(wall3)) { 
                box.x+=6;

            } else if (box.hitTestObject(wall4)) {
                box.x-=6;
            }

    }

    // function keyDownEvent ------------------------------------------------------------
     function keyDownEvent(event:KeyboardEvent):void {
         switch (event.keyCode) {
            case Keyboard.UP:
               keyUpward = true;
               keyDownward = false;
               break;

            case Keyboard.DOWN:
               keyDownward = true;
               keyUpward = false;
               break;

            case Keyboard.LEFT:
               keyLeft = true;
               keyRight = false;
               break;

            case Keyboard.RIGHT:
               keyRight = true;
               keyLeft = false;
               break;
         }
      }

    // function keyUpEvent ------------------------------------------------------------
     function keyUpEvent(event:KeyboardEvent):void {
         switch (event.keyCode) {

            case Keyboard.UP:
               keyUpward = false;
               break;

            case Keyboard.DOWN:
               keyDownward = false;
               break;

            case Keyboard.LEFT:
               keyLeft = false;
               break;

            case Keyboard.RIGHT:
               keyRight = false;
               break;
         }
      }

    // function frameloop ------------------------------------------------------------
     function frameloop(event:Event):void {

         if (keyUpward) {
            forwardMove -=2;
         }
         if (keyDownward) {
            forwardMove += 2;
         }
         if (keyLeft) {
            sideMove -= 2;
         }
         if (keyRight) {
            sideMove += 2;
         }

         forwardMove +=(0-forwardMove)/inertia;
         sideMove +=(0-sideMove)/inertia;

         box.y+=forwardMove;
         box.x+=sideMove;

      } 
+2  A: 

Try listening to the position of your MovieClip with an enterframe event , when the MovieClip is out of bounds , you could remove the keyboard event listener or trigger an opposite keyboard event and make the MovieClip bounce in the opposite direction

Please note that it may conflict with your hitWall() method so you may end up having to use one solution over the other

PatrickS
A: 

Basically, every time you move the object, you must test to see if it is colliding with the wall. If it is then you must set it's position to just inside the "safe" area. You would do these test at the bottom of your frameloop.

TandemAdam