• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker My player can't fall out of the room!

J

Jon Kennel

Guest
I have set up a tilemap collision system in my 2D platformer using GMWolf's "Tilemap collisions in GMS 2" tutorial. The tile collision system works just fine.

I want to make my player die when he falls into pits; so I tried to create an outside room event in my obj_player.
Code:
/// Outside_room_event
// Destroys player when outside room
instance_destroy();
I thought this would work; but there is a slight problem.
Capture.PNG


(The picture is not working but it just shows my player being able to walk along the bottom of the room even though there is no tile for him to be standing on)
My player cannot actually fall outside the room anymore and therefore cannot be destroyed.

I don't understand why this is happening. I've tried analyzing the tile collision script to find a reason but I haven't found anything. My player was previously able to fall outside of the room when I was using the place_meeting functions for collision.

Here is my tilemap collision script
Code:
// Checks for (x,y) points below the player bounding box against the tilemap
var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom + 1) & tile_index_mask;
var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom + 1) & tile_index_mask;


// If one of the tile points are true, then allow for jumping
if (t1 != 0 || t2 != 0) {
    if (keyboard_check_pressed(vk_up)) {
        vsp -= jump_height;
    }
}

  

// Applies gravity
vsp += grav;

// Vertical collision
y += vsp;

if (vsp > 0) { //downwards
    // Checks for points on player's bbox
    var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom) & tile_index_mask;
    var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom) & tile_index_mask;
    
    if (t1 != 0 || t2 != 0) {
        y = ((bbox_bottom & ~15) - 1) - sprite_bbox_bottom;
        vsp = 0;
    }   
} else { //upwards
    var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_top) & tile_index_mask;
    var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_top) & tile_index_mask;
    
    if (t1 != 0 || t2 != 0) {
        y = ((bbox_top + 16) & ~15) - sprite_bbox_top;
        vsp = 0;
    }   
}

// Horizontal collision
x += hsp;

if (hsp > 0) { //downwards
    var t1 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_top) & tile_index_mask;
    var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom) & tile_index_mask;
    
    if (t1 != 0 || t2 != 0) {
        x = ((bbox_right & ~15) - 1) - sprite_bbox_right;
        //hsp = 0;
    }   
} else { //upwards
    var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_top) & tile_index_mask;
    var t2 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom) & tile_index_mask;
    
    if (t1 != 0 || t2 != 0) {
        x = ((bbox_left + 16) & ~15) - sprite_bbox_left;
        //hsp = 0;
    }   
}
And here is the code inside of my player object
Code:
/// Initializes the player
// Variables
hsp = 0;
vsp = 0;
accel = .5;
max_hsp = 2;
jump_height = 5.5;
frict = .9;
grav = .25;

// Tile map info
var l = layer_get_id("collision_map");
tilemap = layer_tilemap_get_id(l);

// Sprite info
sprite_bbox_left = sprite_get_bbox_left(sprite_index) - sprite_get_xoffset(sprite_index);
sprite_bbox_right = sprite_get_bbox_right(sprite_index) - sprite_get_xoffset(sprite_index);
sprite_bbox_bottom = sprite_get_bbox_bottom(sprite_index) - sprite_get_yoffset(sprite_index);
sprite_bbox_top = sprite_get_bbox_top(sprite_index) - sprite_get_yoffset(sprite_index);

// Sets default state
state = scr_player_idle;
Code:
// Executes state
script_execute(state);

- All sprites and tiles are sized 16x16.
- Player origin is centered
- My room size is 1536x216
- I'm using Gamemaker Studio 2

If you need any more information, don't hesitate to ask.

Thanks.
 

rIKmAN

Member
I have set up a tilemap collision system in my 2D platformer using GMWolf's "Tilemap collisions in GMS 2" tutorial. The tile collision system works just fine.

I want to make my player die when he falls into pits; so I tried to create an outside room event in my obj_player.
Code:
/// Outside_room_event
// Destroys player when outside room
instance_destroy();
I thought this would work; but there is a slight problem.
View attachment 16875


(The picture is not working but it just shows my player being able to walk along the bottom of the room even though there is no tile for him to be standing on)
My player cannot actually fall outside the room anymore and therefore cannot be destroyed.

I don't understand why this is happening. I've tried analyzing the tile collision script to find a reason but I haven't found anything. My player was previously able to fall outside of the room when I was using the place_meeting functions for collision.

Here is my tilemap collision script
Code:
// Checks for (x,y) points below the player bounding box against the tilemap
var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom + 1) & tile_index_mask;
var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom + 1) & tile_index_mask;


// If one of the tile points are true, then allow for jumping
if (t1 != 0 || t2 != 0) {
    if (keyboard_check_pressed(vk_up)) {
        vsp -= jump_height;
    }
}

 

// Applies gravity
vsp += grav;

// Vertical collision
y += vsp;

if (vsp > 0) { //downwards
    // Checks for points on player's bbox
    var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom) & tile_index_mask;
    var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom) & tile_index_mask;
   
    if (t1 != 0 || t2 != 0) {
        y = ((bbox_bottom & ~15) - 1) - sprite_bbox_bottom;
        vsp = 0;
    }  
} else { //upwards
    var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_top) & tile_index_mask;
    var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_top) & tile_index_mask;
   
    if (t1 != 0 || t2 != 0) {
        y = ((bbox_top + 16) & ~15) - sprite_bbox_top;
        vsp = 0;
    }  
}

// Horizontal collision
x += hsp;

if (hsp > 0) { //downwards
    var t1 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_top) & tile_index_mask;
    var t2 = tilemap_get_at_pixel(tilemap, bbox_right, bbox_bottom) & tile_index_mask;
   
    if (t1 != 0 || t2 != 0) {
        x = ((bbox_right & ~15) - 1) - sprite_bbox_right;
        //hsp = 0;
    }  
} else { //upwards
    var t1 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_top) & tile_index_mask;
    var t2 = tilemap_get_at_pixel(tilemap, bbox_left, bbox_bottom) & tile_index_mask;
   
    if (t1 != 0 || t2 != 0) {
        x = ((bbox_left + 16) & ~15) - sprite_bbox_left;
        //hsp = 0;
    }  
}
And here is the code inside of my player object
Code:
/// Initializes the player
// Variables
hsp = 0;
vsp = 0;
accel = .5;
max_hsp = 2;
jump_height = 5.5;
frict = .9;
grav = .25;

// Tile map info
var l = layer_get_id("collision_map");
tilemap = layer_tilemap_get_id(l);

// Sprite info
sprite_bbox_left = sprite_get_bbox_left(sprite_index) - sprite_get_xoffset(sprite_index);
sprite_bbox_right = sprite_get_bbox_right(sprite_index) - sprite_get_xoffset(sprite_index);
sprite_bbox_bottom = sprite_get_bbox_bottom(sprite_index) - sprite_get_yoffset(sprite_index);
sprite_bbox_top = sprite_get_bbox_top(sprite_index) - sprite_get_yoffset(sprite_index);

// Sets default state
state = scr_player_idle;
Code:
// Executes state
script_execute(state);

- All sprites and tiles are sized 16x16.
- Player origin is centered
- My room size is 1536x216
- I'm using Gamemaker Studio 2

If you need any more information, don't hesitate to ask.

Thanks.
What state is the player in when he is walking along the bottom of the room, and what is the code for that script?

If there are no tiles at the bottom of the pits, then there can be no tile collision to stop him falling so you must have some code somewhere else that is preventing him from going outside the bounds of the room.
 
J

Jon Kennel

Guest
What state is the player in when he is walking along the bottom of the room, and what is the code for that script?

If there are no tiles at the bottom of the pits, then there can be no tile collision to stop him falling so you must have some code somewhere else that is preventing him from going outside the bounds of the room.
Thanks for the reply. I'm sorry for being so late to respond; I've been away from my computer.

Anyway, my player has only one state as of now, that being the 'scr_player_idle' state.
Code:
// Gets player input
var h_input = keyboard_check(vk_right) - keyboard_check(vk_left);
var up = keyboard_check_pressed(vk_up);
var up_released = keyboard_check_released(vk_up);

// Moves player
if (h_input != 0) {
    hsp += h_input*accel;
    hsp = clamp(hsp,-max_hsp,max_hsp);
} else {
    // Applies friction
    hsp = lerp(hsp,0,frict);
}

// Flips player sprite
if (hsp != 0) {
    image_xscale = sign(hsp);
}

// tile collision
scr_tile_collision();
 
Top