2016-05-24 25 views
1

所以這是一個我目前正在研究的遊戲,我碰到了工作和移動的碰撞,但是當我碰撞到任一側牆(從牆上列表)停止移動我的球員,但如果我一直按住鍵,玩家將通過C#碰撞,如果一個布爾值表示他不能移動,如何讓玩家不能移動

這完全移動的是移動和碰撞的代碼:

bool left = false; 
    bool up = false; 
    bool right = false; 
    bool down = false; 

    private void Form1_KeyDown(object sender, KeyEventArgs e) 
    { 
     #region Movement 
     if (e.KeyCode == Keys.D && !right) 
     { 
      right = true; 
     } 

     if (e.KeyCode == Keys.A && !left) 
     { 
      left = true; 
     } 

     if (e.KeyCode == Keys.W) 
     { 
      up = true; 
     } 

     if (e.KeyCode == Keys.S) 
     { 
      down = true; 
     } 
     #endregion 
    } 

    public void Collision() 
    { 
     for (int x = 0; x < walls.Count; x++) 
     { 
      if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && right) 
      { 
       right = false; 
      } 

      if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && left) 
      { 
       left = false; 
      } 

      if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && up) 
      { 
       up = false; 
      } 

      if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && down) 
      { 
       down = false; 
      } 
     } 
    } 

    private void Form1_KeyUp(object sender, KeyEventArgs e) 
    { 
     if (e.KeyCode == Keys.D) 
     { 
      right = false; 
     } 

     if (e.KeyCode == Keys.A) 
     { 
      left = false; 
     } 

     if (e.KeyCode == Keys.W) 
     { 
      up = false; 
     } 

     if (e.KeyCode == Keys.S) 
     { 
      down = false; 
     } 
    } 

    private void timer1_Tick(object sender, EventArgs e) 
    { 
     #region True 
     if (right) 
     { 
      Point loc = player.obj.Location; 
      loc.X = loc.X + 4; 
      player.obj.Location = loc; 
     } 

     if (left) 
     { 
      Point loc = player.obj.Location; 
      loc.X = loc.X - 4; 
      player.obj.Location = loc; 
     } 

     if (up) 
     { 
      Point loc = player.obj.Location; 
      loc.Y = loc.Y - 4; 
      player.obj.Location = loc; 
     } 

     if (down) 
     { 
      Point loc = player.obj.Location; 
      loc.Y = loc.Y + 4; 
      player.obj.Location = loc; 
     } 
     #endregion 

     #region NotTrue 
     if (!right) 
     { 
      Point loc = player.obj.Location; 
      loc.X = loc.X + 0; 
      player.obj.Location = loc; 
     } 

     if (!left) 
     { 
      Point loc = player.obj.Location; 
      loc.X = loc.X - 0; 
      player.obj.Location = loc; 
     } 

     if (!up) 
     { 
      Point loc = player.obj.Location; 
      loc.Y = loc.Y - 0; 
      player.obj.Location = loc; 
     } 

     if (!down) 
     { 
      Point loc = player.obj.Location; 
      loc.Y = loc.Y + 0; 
      player.obj.Location = loc; 
     } 
     #endregion 

     Collision(); 
    } 
} 
} 

提醒:一切正常,但我的球員在與對象碰撞並停止幾秒鐘後,將繼續穿過牆壁。

回答

0

你會需要像cantMoveRight另一個布爾防止玩家向右移動時,他們碰了壁,例如:

if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && right) 
{ 
    right = false; 
    cantMoveRight = true; 
    cantMoveLeft = false; //Assuming when they cant move right they must move left, perhaps check this in situations when the player can only move forward or backward 
} 

if (player.obj.Bounds.IntersectsWith(walls[x].Bounds) && left) 
{ 
    left = false; 
    cantMoveRight = false; 
    cantMoveLeft = true; 
} 

然後在KeyUp事件檢查:

if (e.KeyCode == Keys.D && !cantMoveRight) 
{ 
    right = false; 
} 

if (e.KeyCode == Keys.A && !cantMoveLeft) 
{ 
    left = false; 
} 

通過這種方式,當觸發/觸發事件時,玩家不會穿過牆壁。

+0

好吧,我會在早上醒來時嘗試這種方法。 –

相關問題