2012-01-22 12 views
-2

2つの別々のオブジェクトの2つの矩形が交差しているかどうかを確認しようとしています。残念ながら、それは動作していません。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

あなたがプレイヤークラスで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