Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
    
I'd like to see how you initialize your tile.rectangle –  Blau May 5 '12 at 20:37
    
I'm initializing it inside the Tile class constructor. –  Cosmin Negoita May 5 '12 at 20:38
    
you have the problem in your tile class, maybe you have declared position or rectangle as static... –  Blau May 5 '12 at 20:46
    
None of the are declared as static. They are only made public. –  Cosmin Negoita May 5 '12 at 20:51
    
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... –  Blau May 5 '12 at 20:57
add comment

1 Answer

up vote 2 down vote accepted

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

share|improve this answer
    
Thanks, it solved everything! –  Cosmin Negoita May 6 '12 at 7:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.