2016-05-24 6 views
1

Also das ist ein Spiel, an dem ich gerade arbeite, und ich habe Kollisionen zu arbeiten und zu bewegen, aber wenn ich auf beiden Seiten kollidiere eine Wand (aus der Liste Wänden) stoppt den Player bewegt, aber wenn ich den Schlüssel immer gedrückt hält, wird der Spieler vollständig durchC# Kollision, wie man den Spieler nicht bewegt, wenn ein Boolescher sagt, er kann sich nicht bewegen

Dies ist der Code bewegt zum bewegen und Kollision:

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(); 
    } 
} 
} 

Erinnerung: Alles funktioniert, aber mein Spieler wird, nachdem er mit dem Objekt kollidiert ist und für ein paar Sekunden stehen bleibt, sich durch die Wand bewegen.

Antwort

0

Sie gehen einen anderen boolean wie cantMoveRight brauchen die Spieler rechts bewegen zu verhindern, wenn sie gegen eine Wand, zum Beispiel:

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; 
} 

Dann im KeyUp Ereignis, dass der Check:

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

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

Auf diese Weise bewegt sich der Spieler nicht durch die Wand, wenn das Ereignis timer1_Tick ausgelöst wird.

+0

Ok, ich werde diese Methode am Morgen versuchen, wenn ich aufwache. –

Verwandte Themen