0
\$\begingroup\$

What I'm saying is, let's say I have a list. That list contains all the tiles in the game. I use this to update them all, draw them all, etc.

for (var i = 0; i < Tiles.Count; i++)
{
     Tiles[i].Update(gameTime);
}

But how can I handle collision from each side of the tile, each side of the player for all tiles, but instead have each tile have its own individual set of collision codes? Because if I have the player box intersect the tile box, it counts as ALL of the tiles..

\$\endgroup\$
3
  • \$\begingroup\$ Perhaps make different classes for the different types of Tiles, and have them extend the Tile class and implement their own Update function. \$\endgroup\$ – dimitris93 Feb 11 '16 at 12:27
  • \$\begingroup\$ Well there's one type of tile, and I wanna put in collision for each and every one of them. Like there could be 100 tiles I don't wanna make a class for each of them \$\endgroup\$ – jymas Feb 11 '16 at 12:32
  • 1
    \$\begingroup\$ I think I didn't quite understand the question then. You mentioned that "each tile have its own individual set of collision codes". That made me think that you have a few different type of tiles. If you only have a single type of tiles I don't quite understand what you are having trouble with. Checking the collision from each side ? That seems a bit trivial... I think you might want to save the object's previous position to calculate that easier. \$\endgroup\$ – dimitris93 Feb 11 '16 at 14:41
1
\$\begingroup\$

Assuming you store each tiles x, y, width and height in the class, you could provide a method which makes a rectangle and then returns it, and then check if that intersects with the rectangle of the player.

for (var i = 0; i < Tiles.Count; i++)
       {
            Tiles[i].Update(gameTime);
            if (Tiles[i].getRectangle().intersect(Player.getRectangle()))
            {
                //get more details and resolve collision
            }
       }

The getRectangle method would just consist of

public Rectangle getRectangle() {
    return new Rectangle(x, y, width, height);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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