2010-09-22 89 views
1

我有一個可以向上,向下,向左和向右移動的MovieClip。如果MovieClip碰到牆上,MC會彈跳,不應該移動過牆壁 - 但如果您繼續按下上,下,左或右鍵,並且不放開 - MovieClip將會越過牆壁。我試圖找出一種辦法,阻止這種情況發生。 link of movement設置MovieClip(AS3)的邊界限制

// 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

試着聽聽你的影片剪輯與enterFrame事件,位置時,影片剪輯是出界,你可以卸下鍵盤事件監聽器或觸發相反的鍵盤事件,並在該影片剪輯反彈相反的方向

請注意,它可能與你的hitWall)衝突(方法,所以你可能最終不得不使用一個解決方案比其他

0

基本上,每次移動的對象時,你必須測試,看看它正在與牆壁相撞。如果是,那麼您必須將其設置在「安全」區域內。你會在你的frameloop的底部做這些測試。