I'm reading the book "Learining XNA 4.0", and I'm in the object oriented disgn part. I'm having a weird problem with the collision of 2 rectangles.
I have a list of automateSprite and a Player class both derived from the class Sprite. in the update method im checking if player and the aotomatedSprite rectangles are touching each other, now when I go over the list I have a string called touching that represent the collision.
My problem is the variable touching
only changes if the player Sprite touches the last automateSprite in the list.
The code that the book offers for testing is to do Game.Exit()
if any collision was found. that work on every automatedSprite in the list, but when I change it to my simple test, it acts like I only check the last item from a list of 4 automatedSprite.
Here is the code:
string touching = "";
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
player.Update(gameTime, Game.Window.ClientBounds);
foreach (Sprite sprite in spriteList)
{
sprite.Update(gameTime, Game.Window.ClientBounds);
if (sprite.collisionRect.Intersects(player.collisionRect))
touching = "touching";
else
touching = "not touching";
}
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend);
spriteBatch.DrawString(font, touching, Vector2.Zero, Color.Red);
player.Draw(gameTime, spriteBatch);
foreach (Sprite sprite in spriteList)
{
sprite.Draw(gameTime, spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
(sorry for my English)