Tile-based platformer collision detection. For simplicity, we assume you only have two types of tiles to deal with: an air tile (empty space), and a ground tile (the tiles you want to check for collision).
These are arranged in a two-dimensional array.
This is simply one way to do collision detection. You may find a better method, especially if key game concepts are different.
//First, obtain the x-value of a tile a few tiles behind the player, and ahead of the player.
//Similarly, obtain the y-value of a tile a few tiles above and below the player.
col = false;
for (all tiles having a y-value you are interested in)
for (all tiles having an x-value you are interested in)
if(the player is hitting a tile from any side*)
if(this is a ground tile)
if(a point at the center-bottom of the player is hitting the block AND falling == true)
player.y = such that you are naturally standing on the block;
falling = false;
vertical speed = 0;
col = true;
end if
if(a point at the center-top of the player is hitting the block AND jumping == true)
jumping = false;
falling = true;
player.y = such that you are not intersecting with the block;
vertical speed = at least 0. I have 1.;
end if
if(a point on the left side of the player is hitting the block)
horiz. speed = 0;
player.x = such that you are not intersecting with the block;
end if
if(a point on the right side of the player is hitting the block)
horiz. speed = 0;
player.x = such that you are not intersecting with the block;
end if
end if
end if
end for
end for
if(col == false)
falling = true;
vertical speed = vertical speed + vertical acceleration;
end if
*For flash, you can use hitTestObject easily. For other languages, you might have to hack out what this means for yourself. Use knowledge of widths, heights, and x and y values. If both objects are rectangles, you can do this pretty easily - just think about it.