I am aware that Randomousecrap recently did an extensive tutorial on this topic, and i have not tried it just yet, but i will show you how to implement easy collision in just about 4 lines of code. This collision if practically perfect and can be applied to pretty much any type of 2d game, from RPGs to Platformers. The only thing that i will say is somewhat buggy is outer corners of tiles, but it’s super minimal and unnoticeable with actual gameplay. We will be using a sprite for the player and BG tiles & layers for what we be colliding with.
The Basics
You will be using a sprite of any size for this to work, it does not matter the size just requires some number adjusting which is not hard. BG tiles will act as our map, realistically you only need one layer for this but if you want to have a layer in which the player may interact with things that works with this too, just some modifications to it will be optimal. We want the sprite to collide with BG tiles, so we just have to check the BG tiles where the player is in screen coordinates.
The code
So we have to use the BGGET command, that is not too hard. It’s syntax is BGGET(Layer#, Screen X, Screen Y[, Coordinate system flag]) For Layer# choose the layer with all he stuff you want the player to collide with. Screen X and Screen Y is where the player is in screen coordinates (0-399,0-239). And set Coordiante system flag to one, this tells the system to check screen coordinates not any other type of coordinates. It sholuld something like:
BGGET(1,200,120,1)
We are saying “Detect BG tiles in the center of the screen”
Now we must put this into an IF statement. This will ask the system every frame if there is a BG tile here on layer# do this.
IF BGGET(1,200,120,1) THEN
Now if we detect something in the center of the screen ( making our statement true ) we can tell it to do something.Now before we go further we must understand we have to make collission for every side of the Layer, so we can have collission around all sides of the tiles.I find the sweet spot is to add or subtract 7 from the X or the Y coordinate in the BGGET command. For this portion of the collision i will be using collision for the top of the BG tiles, but it is applicable to all sides of the tile.
For adding/subtracting 7 from the BGGET command here is a quick guide showing you where you should add and subtract 7 as well as if your going to do so on the X or the Y:
TOP:
BGGET(Layer#,X,Y+7,1)
BOTTOM:
BGGET(Layer#,X,Y-7,1)
RIGHT:
BGGET(Layer#,X+7,Y,1)
LEFT:
BGGET(Layer#,X-7,Y,1)
Ok now let’s add in what will happen when there is a tile within the coordinates we have set on our screen.
IF BGGET(1,200,120+7,1) THEN DEC PY,1
PY would be Player Y.
You will have noticed i used DEC and PY for this one, this is specific to top collision, you’d use other combinations for the other sides:
BOTTOM:
INC PY,1
LEFT:
INC PX,1
RIGHT:
DEC PX,1
A complete collision block should look like:
IF BGGET(1,200,120+7,1) THEN DEC PY,1
IF BGGET(1,200,120-7,1) THEN INC PY,1
IF BGGET(1,200+7,120,1) THEN INC PX,1
IF BGGET(1,200-7,120,1) THEN DEC PX,1