2017-11-07 8 views
-3

と交差する

public Block IsColliding(Texture2D texture) 
    { 
     foreach (Block block in currentWorld.blocks) 
     { 
      if (texture.Bounds.Intersects(block.texture.Bounds)) return block; 
     } 
     return null; 
    } 

私は、プレイヤーが世界で何と衝突されたかどうかを確認するために、このコードを使用して衝突していない間も毎回trueを返し交差機能

+0

「Intersects」のオーバーロードが見つかりません[https://msdn.microsoft.com /en-us/library/microsoft.xna.framework.rectangle.intersects.aspx]にはTexture2Dと 'Rectangle'があります。また、オブジェクトのメソッドを呼び出すと、そのオブジェクトをパラメータとして渡すことは奇妙に思われます。これはあなたが使用している_exact_コードですか? –

+0

今すぐコードをチェックして、何かを偶発的に追加しました – Ahmedm

+0

デバッガで実行して、衝突していないときの四角形の境界線を確認しましたか? –

答えて

0

私はそれを使用して停止し、代わりに、私は
自分の数学を使用し、ここでのコードは、誰かがそれ

public bool Intersects(Vector2 texture1, Vector2 textureSize1, Vector2 texture2, Vector2 textureSize2) 
    { 
     if (texture1.X < texture2.X + textureSize2.Y && texture1.X + textureSize1.X > texture2.X && texture1.Y < texture2.Y + textureSize2.Y && texture1.Y + textureSize1.Y > texture2.Y) return true; 
     return false; 
    } 

    public Block IsColliding(Vector2 position, float width, float height) 
    { 
     foreach (Block block in currentWorld.blocks) 
     { 
      if (Intersects(position, new Vector2(width, height), block.position, new Vector2(block.texture.Bounds.Width, block.texture.Bounds.Height))) return block; 
     } 
     return null; 
    } 

たい場合は、最初のベクトル2(位置)プレーヤーの位置です。2つの浮動小数点は、プレーヤーテクスチャの幅と高さです(player.texture.Bounds.Width/Height)