-
Notifications
You must be signed in to change notification settings - Fork 62
Collisions
Zalo edited this page Jul 25, 2019
·
4 revisions
Right now if you move the player you'll see it can go throught walls (also going offscreen will kill the sprite so we need to avoid this). Here is how to add collision checking with background tiles:
- Collisionable tiles are declared as an array of UINT8 ended with 0. If you open the file tiles.gbr you will see that we only have two tiles (the first one is empty and we don't want that to be collidable). Declare the next array on StateGame.c
UINT8 collision_tiles[] = {1, 0};
- This list is the 4th paramater passed to the InitScroll function on the Start method of StateGame. Right now it is set to 0 (null), change it into this:
InitScroll(mapWidth, mapHeight, map, collision_tiles, 0, 3);
- Now if you want the scroll to manage collisions with your sprite you need to use the function TranslateSprite instead of directly updating the x and y coordinates of the sprite. Here is how the Update method should look like now:
void Update_SpritePlayer() {
if(KEY_PRESSED(J_UP)) {
TranslateSprite(THIS, 0, -1);
SetSpriteAnim(THIS, anim_walk, 15);
}
if(KEY_PRESSED(J_DOWN)) {
TranslateSprite(THIS, 0, 1);
SetSpriteAnim(THIS, anim_walk, 15);
}
if(KEY_PRESSED(J_LEFT)) {
TranslateSprite(THIS, -1, 0);
SetSpriteAnim(THIS, anim_walk, 15);
}
if(KEY_PRESSED(J_RIGHT)) {
TranslateSprite(THIS, 1, 0);
SetSpriteAnim(THIS, anim_walk, 15);
}
if(keys == 0) {
SetSpriteAnim(THIS, anim_idle, 15);
}
}
- There is still a small issue that can be fixed. Unless you did a square your Sprite might be stopping before touching the collidable tile
this can be fixed by adjusting the collider of the Sprite on the Start method. The collider of an Sprite is defined by an rectangle declared using coll_x, coll_y, coll_w and coll_h These values worked for me:
void Start_SpritePlayer() {
THIS->coll_x = 2;
THIS->coll_y = 0;
THIS->coll_w = 12;
THIS->coll_h = 16;
}