2016-08-14 21 views
0

皆さん、私はC#/ XNAでちょっと試合をしています。 と私はこの問題が衝突しています。C#Rectangle Collision Detectionが正常に動作しません

私は自分のコードで何をしているのですか?通常のプレイヤームーブメントを作っています。たとえば、アップキーが押されたかどうかをチェックするのではなく、Player CanGoTop == true;

if (keyState.IsKeyDown(Keys.Up) && CanGoTop == true) 
{ 
bla bla bla 
} 

私が右、上、下、左のためにこれを行う、とbolleanがfalseの場合、プレイヤーはxと.Y、それ以外のなステートメントは、速度を設定しますmovemntのものを4たstatments後にあります(移動することはありません〜0)

私はPlayerクラスの中でメソッドを作ったので、あなたはその考えを得るでしょう。 それを見てみるだけで、何が起こっているのかを説明しなければならないと思います。

 public void RectangleInteraction(Rectangle anotherRectangle) 
    { 
     if (playerRectangle.Bottom <= anotherRectangle.Top && 
      playerRectangle.Top >= (anotherRectangle.Top - playerRectangle.Height) && 
      playerRectangle.Left <= anotherRectangle.Right && 
      playerRectangle.Right >= anotherRectangle.Left) 
     { 
      CanGoBot = false; 
     } 
     else CanGoBot = true; 


     if (playerRectangle.Top >= anotherRectangle.Bottom && 
      playerRectangle.Bottom <= (anotherRectangle.Bottom + playerRectangle.Height) && 
      playerRectangle.Left <= anotherRectangle.Right && 
      playerRectangle.Right >= anotherRectangle.Left) 
     { 
      CanGoTop = false; 
     } 
     else CanGoTop = true; 


     if (playerRectangle.Top <= anotherRectangle.Bottom && 
      playerRectangle.Bottom >= anotherRectangle.Top && 
      playerRectangle.Right <= anotherRectangle.Left && 
      playerRectangle.Left >= (anotherRectangle.Left - playerRectangle.Width)) 
     { 
      CanGoRight = false; 
     } 
     else CanGoRight = true; 


     if (playerRectangle.Top <= anotherRectangle.Bottom && 
      playerRectangle.Bottom >= anotherRectangle.Top && 
      playerRectangle.Left >= anotherRectangle.Right && 
      playerRectangle.Right <= (anotherRectangle.Right + playerRectangle.Width)) 
     { 
      CanGoLeft = false; 
     } 
     else CanGoLeft = true; 

    } 

次に、私のGame1.cs Update()メソッドでは、私のプレーヤーinstaそのメソッドを呼び出すnce、そして今私はたくさんのことを盗んでいるものです。CanGoLeftでは動作しますが、他の3つのboolでは動作しません。

なぜ私はInGameMsgsを使って私の4スクリーンショットを手伝ってくれるのですか?私のコードでこれらのメッセージをチェックすると、CollisionLogicは良いと言われますが、何か間違っています。 なぜCanGoLeftだけが動作しますか? :/

あなたの時間と助けを進んでいただきありがとうございます。

CanGoLeft CanGoBot

答えて

1

あなたのロジックは、いくつかの点で壊れているようです。 RectangleのメソッドをOffsetIntersectsのように使用して、ターゲットRectangleがコロイド化するかどうかを確認する必要があります。私はこれがあなたの意図に近づくと思います(distanceと仮定すると、あなたがプレイヤーの動きをしているそれによって値です):.NET Frameworkの他のRectangle構造体が存在し、これはあなたがMicrosoft.Xna.Framework.Rectangleを使用していると仮定している(

public void RectangleInteraction(Rectangle anotherRectangle) 
{ 
    Rectangle down = playerRectangle; down.Offset(0, distance); 
    Rectangle up = playerRectangle; up.Offset(0, -distance); 
    Rectangle left = playerRectangle; left.Offset(-distance, 0); 
    Rectangle right = playerRectangle; right.Offset(distance, 0); 

    ConGoBot = !down.Intersects(anotherRectanlge); 
    ConGoTop = !up.Intersects(anotherRectanlge); 
    ConGoLeft = !left.Intersects(anotherRectanlge); 
    ConGoRight = !right.Intersects(anotherRectanlge); 
} 

、あなたの質問にはというタグが付けられました)

関連する問題