I'm making a 2D platformer and I'm using tiles as the ground. I have a Tile class and I'm loading the tiles with the help of a list in the main game class:

public static List<Tile> tiles = new List<Tile>();

The following is how I load the tiles from the list with 2 for loops in the LoadContent method:

for (int i = 0; i<=20; i++)
            tiles.Add(new Tile(Content.Load<Texture2D>("tile"), new Vector2(i * 16, 304)));
        for (int i = 0; i <= 10; i++)
            tiles.Add(new Tile(Content.Load<Texture2D>("tile"), new Vector2(i * 16, 288)));

Now, I wanna apply some kind of gravity and collision, still it's just something primitive. I'm applying these inside a Player class within a foreach loop like this:

foreach (Tile tile in Game1.tiles)
            if (isInAir == true) { velocity.Y = 100f; }
            else { velocity.Y = 0f; }

            if (recBot.Intersects(tile.rectangle)) colBot = true;
            else colBot = false;

        if (colBot == true) isInAir = false;
        else isInAir = true;

The issue is that the collision occurs only on the last loaded tile by the for loop in the LoadContent method. The sprite stops falling when it collides with the last added tile, but when I'm moving left or right and it intersects with other tiles, it doesn't apply the collision and it falls through them.

I'm kinda new to programming, so take me easy :) Any other things that you need to know, just tell me and I'll add more details or code!

  • \$\begingroup\$ I'd like to see how you initialize your tile.rectangle \$\endgroup\$
    – Blau
    Commented May 5, 2012 at 20:37
  • \$\begingroup\$ I'm initializing it inside the Tile class constructor. \$\endgroup\$ Commented May 5, 2012 at 20:38
  • \$\begingroup\$ you have the problem in your tile class, maybe you have declared position or rectangle as static... \$\endgroup\$
    – Blau
    Commented May 5, 2012 at 20:46
  • \$\begingroup\$ None of the are declared as static. They are only made public. \$\endgroup\$ Commented May 5, 2012 at 20:51
  • \$\begingroup\$ Without see the tile class code... I can't do any more... maybe you should inspect rectangle property of every tile... to know what value has... \$\endgroup\$
    – Blau
    Commented May 5, 2012 at 20:57

1 Answer 1


The problem here is that you are checking the collision for each of the tiles, and setting the single collision flag to true or false based on that. So if the player collides with the first tile, then colbot is true after the first tile. However, you then check the rest of the tiles, where there is no collision, and set colbot to false.

Suggested solution: set colbot to false before the loop, and remove the else from the intersection check (only check if it intersects, and set colbpt to true if it does).

Some additional tips: - once you find a collision and set colbot to true, you could break out of the loop and not bother checking the rest of the tiles - move the vertical speed check/adjustment out of the loop


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .