2012-11-13 186 views
-1

我有一個小坦克和一個黑色的方形程序。坦克應該對用戶輸入做出響應,並在接觸坦克時停下來。但是,窗口出現後(包含盒子和罐子)我沒有用戶輸入。當我按下其中一個箭頭鍵時,坦克根本不動。XNA 4.0 2d碰撞檢測

所以這裏只是我的代碼中一些更重要的例子。

public class TopDownShooter : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    private Texture2D tank, blackSquare; 
    private KeyboardState state; 
    private float angle1 = 0; 
    private float angle2 = 0; 
    private float xLocation, yLocation; 
    Vector2 location1 = new Vector2(900, 400), origin1; 
    Vector2 location2 = new Vector2(400, 400), origin2; 
    Rectangle playerRectangle, obstacleRectangle; 



    /// <summary> 
    /// Allows the game to run logic such as updating the world, 
    /// checking for collisions, gathering input, and playing audio. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (Keyboard.GetState().IsKeyDown(Keys.Escape)) 
      this.Exit(); 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 
     playerRectangle = Move(playerRectangle); 

     base.Update(gameTime); 
    } 


    private Boolean IsNotCollision(Rectangle rectangle1, Rectangle rectangle2) 
    { 
     if (rectangle1.Intersects(rectangle2)) return true; 
     else return false; 
    } 


    /// <summary> 
    /// Sets the velocity of the targeted object based on user keyboard input 
    /// </summary> 
    private Rectangle Move(Rectangle rectangle) 
    { 
     state = Keyboard.GetState(); 
     xLocation = rectangle.X; 
     yLocation = rectangle.Y; 

     if (IsNotCollision(obstacleRectangle, playerRectangle)) 
     { 
      if (state.IsKeyDown(Keys.Left)) rectangle.X += -4; 
      if (state.IsKeyDown(Keys.Right)) rectangle.X += 4; 
      if (state.IsKeyDown(Keys.Up)) rectangle.Y += -4; 
      if (state.IsKeyDown(Keys.Down)) rectangle.Y += 4; 
     } 
     return rectangle; 
    } 



    /// <summary> 
    /// This is called when the game should draw itself. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     spriteBatch.Begin(); 

     playerRectangle = new Rectangle(0, 0, tank.Width, tank.Height); 
     origin1 = new Vector2(tank.Width/2, tank.Height); 
     obstacleRectangle = new Rectangle(0, 0, blackSquare.Width, blackSquare.Height); 
     origin2 = new Vector2(blackSquare.Width/2, blackSquare.Height); 

     spriteBatch.Draw(tank, location1, playerRectangle, Color.White, angle1, origin1, 1.0f, SpriteEffects.None, 1); 
     spriteBatch.Draw(blackSquare, location2, obstacleRectangle, Color.White, angle2, origin2, 1.0f, SpriteEffects.None, 1); 

     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 

回答

1

編輯:

其實..你永遠不似乎更新location1 ...

+0

這並不提供答案的問題。要批評或要求作者澄清,請在其帖子下方留言。 – VMAtm

+0

我確實有一個更大的答案..但它看起來像OP的一個小疏忽,他們實際上並沒有更新位置。因此,這是我的答案。 –

+0

這是正確的答案。 Jonheel正在每個畫面中的相同位置抽取坦克。 – jlvaquero