I've made a very simple collision detection, it works fine. However, when I decided to load in all my rectangles into a list and then give each of them collision, it only adds it to the first one. Any ideas? I've tried it with a for loop as well.
Here's the code:
Rectangle interaction, interaction2, interaction3;
List<Rectangle> collisionList = new List<Rectangle>();
public GamePlayScreen()
{
interaction = new Rectangle(200, 200, 32, 32);
interaction2 = new Rectangle(400, 200, 32, 32);
interaction3 = new Rectangle(200, 400, 32, 32);
collisionList.Add(interaction);
collisionList.Add(interaction2);
collisionList.Add(interaction3);
}
// This right here is just to show you guys that it is getting updated.
// This is not the real update, I refrained from posting that since it might confuse
// people.
public override Update(GameTime gameTime)
{
CheckCollision();
}
float xx;
float yy;
private void
CheckCollision()
{
foreach (Rectangle rect in collisionList)
{
if (player.Sprite.Bounds.Y > rect.Top - 33 && player.Sprite.Bounds.Y < rect.Bottom && player.Sprite.Bounds.X < rect.Right && player.Sprite.Bounds.X > rect.Left - 30)
{
player.Sprite.position.Y = yy;
}
if (player.Sprite.Bounds.Y < rect.Bottom && player.Sprite.Bounds.Y > rect.Top && player.Sprite.Bounds.X < rect.Right && player.Sprite.Bounds.X > rect.Left - 30)
{
player.Sprite.position.Y = yy;
}
if (player.Sprite.Bounds.X > rect.Left - 30 && player.Sprite.Bounds.X < rect.Right && player.Sprite.Bounds.Y > rect.Top - 30 && player.Sprite.Bounds.Y < rect.Bottom)
{
player.Sprite.position.X = xx;
}
else
{
xx = player.Sprite.position.X;
yy = player.Sprite.position.Y;
}
}
}
//The Draw method
spriteBatch.Draw(scrollDownButtonNormal, interaction, Color.White);
spriteBatch.Draw(scrollDownButtonNormal, interaction2, Color.White);
spriteBatch.Draw(scrollDownButtonNormal, interaction3, Color.White);
I will of course continue to work on this, and post a solution if I find one.
~Etarnalazure
UPDATE Just checked with a boolean if the other boxes react, it changes to true. So it means it works just fine (I put the boolean in between each if loop to check if it registered any collision).