2012-01-22 67 views
-2

我想看看兩個分開的對象的兩個矩形是否相交。不幸的是,它不工作。下面是代碼:兩個矩形的交叉點

球員

class Player 
{ 
    public static Texture2D texture; 

    public int xPos = 320; 
    public int yPos = 530; 
    public Rectangle rectangle; 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     rectangle = new Rectangle(xPos, yPos, 75, 59); 
     spriteBatch.Draw(texture, rectangle, Color.White); 
    } 

    public void Update(GameTime gameTime) 
    { 
     KeyboardState keyboard = Keyboard.GetState(); 

     if (keyboard.IsKeyDown(Keys.Left)) 
     { 
      xPos -= 3; 
     } 
     if (keyboard.IsKeyDown(Keys.Right)) 
     { 
      xPos += 3; 
     } 
    } 
} 

Asteriod

public class Asteroid : gameObject 
{ 
    int xPos = 0; 
    int yPos = -10; 

    public override void Draw(SpriteBatch spriteBatch) 
    { 
     rectangle = new Rectangle(xPos, yPos, 32, 32); 
     spriteBatch.Draw(texture, rectangle,Color.White); 
    } 

    public override void Update(GameTime gameTime) 
    { 
     yPos++; 
     rectangle = new Rectangle(xPos, yPos, 32, 32);  
    } 

    public Asteroid(int value) 
    { 
     xPos = value; 
    } 
} 

遊戲對象

public abstract class gameObject 
{ 
    public static Texture2D texture; 
    public Rectangle rectangle; 

    public abstract void Draw(SpriteBatch spriteBatch); 
    public abstract void Update(GameTime gameTime); 
} 

遊戲

public class Game1 : Microsoft.Xna.Framework.Game 
{   
    List<gameObject> objectList = new List<gameObject>(); 
    Random rand = new Random(1); 
    Asteroid asteroid; 

    int asteroidCount = 0; 
    Player player = new Player(); 

    protected override void Update(GameTime gameTime) 
    { 
     scorevalue++; 

     player.Update(gameTime); 

     if (rand.Next(0, 8) == 2 && asteroidCount < 50) 
     { 
      for (int i = 0; i < 5; i++) 
      { 
       asteroid = new Asteroid(rand.Next(32,screenWidth)); 
       objectList.Add(asteroid); 
       asteroidCount++; 
      } 
     } 

     foreach (Asteroid asteroid in objectList) 
     { 
      asteroid.Update(gameTime); 
     } 

     for (int i = 0; i < objectList.Count(); i++) 
     { 
      if (objectList[i].rectangle.Intersects(player.rectangle)) 
      { 
       objectList.Remove(asteroid); 
      } 
     } 

     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     // TODO: Add your update logic here 

     base.Update(gameTime); 
    } 


    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     // TODO: Add your drawing code here 
     spriteBatch.Begin(); 
     spriteBatch.DrawString(font, "Score: " + scorevalue, new Vector2(5, 5), Color.White); 
     foreach (Asteroid asteroid in objectList) 
     { 
      asteroid.Draw(spriteBatch); 
     } 
     player.Draw(spriteBatch); 
     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 
} 

所有幫助是極大的讚賞。當我嘗試循環並檢查交叉點時,它不起作用。謝謝。

+0

Urgh ...爲什麼隨機downvote? – YellPika

+0

您可以通過使用調試器逐步執行代碼來輕鬆診斷問題。 – Msonic

回答

1

你試過這個嗎?

Rectangle rect = Rectangle.Intersect(rectangle1, rectangle2); 
if (rect.IsEmpty) 
{ 

} 
+0

這將如何提供不同的結果? –

+0

我不認爲這將工作,因爲我想從列表中刪除特定的小行星。如果這確實起作用,那麼您究竟如何使用remove()方法? – user1162850

+0

@ user1162850你仍然需要在if塊中添加'objectList.Remove(asteroid);' –

0

看起來你忘了在你的Player類中更新rectangle

public void Update(GameTime gameTime) 
{ 
    KeyboardState keyboard = Keyboard.GetState(); 

    if (keyboard.IsKeyDown(Keys.Left)) 
    { 
     xPos -= 3; 
    } 
    if (keyboard.IsKeyDown(Keys.Right)) 
    { 
     xPos += 3; 
    } 

    rectangle.X = xPos; 
} 
+0

我也是這麼想的,但他實際上是在'Draw()'調用它。 –

+0

該死的,你說得對。我誤解了它的構造函數或其他... – YellPika