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!