Legacy GM Slightly glitchy jumping

N

Nirwanda

Guest
Following a tutorial by Shaun Spalding, I created a controlled jump but I've run into a bug: if I hold the jump key just the right amount of time, the character will get stuck on the ground:
Could it be a bounding box issue? Or is there something wrong with my code? or uhh..What else could it be?
Here's the code:
Create event:
Code:
image_speed = 0.2;
grav = 0.25;
hsp = 0;
vsp = 0;
jumpspeed = 8;
movespeed = 8;
global.direction=1
Step event:
Code:
key_jump = keyboard_check_pressed(ord("Z"));
key_jump_held = keyboard_check(ord("Z"));

// HORIZONTAL MOVEMENT
if (keyboard_check(vk_left) && keyboard_check(vk_right)) ||
    (!keyboard_check(vk_left) && !keyboard_check(vk_right))
    // holding both directional keys or neither of them - stop movement
{
    if hsp >= 1 hsp -= 1; // reduce speed
    else if hsp <= -1 hsp += 1; // reduce speed
    else if hsp != 0 hsp = 0; // stop
}
else if keyboard_check(vk_right) // holding RIGHT key
{
    global.direction = 1; // set direction
    hsp = min(hsp + 1, 8); // increase speed; limit to 8
}
else if keyboard_check(vk_left) // holding LEFT key
{
    global.direction = 0; // set direction
    hsp = max(hsp - 1, -8); // increase speed; litit to -8
}

// WHEN ON GROUND
if (place_meeting(x, y+1, obj_ground))
{
    // JUMP
    if key_jump vsp = -jumpspeed // pressing jump button
 
    // SET SPRITE (STANDING/RUNNING ANIMATION)
    if hsp == 0 // when idle
    {
        if global.direction == 1 sprite_index = spr_rose_idleR; // idle: right
        else sprite_index = spr_rose_idleL; // idle: left
    }
    else if hsp > 0 sprite_index = spr_rose_runR; // running: right
    else if hsp < 0 sprite_index = spr_rose_runL; // running: left
 
}

// WHEN IN MID-AIR
else // when in mid-air
{
    // GRAVITY
    vsp = min(vsp + grav, 10) // increase falling speed; limit to 10
 
    // SET SPRITE (JUMPING ANIMATION)
    if vsp <= 0 // going UP
    {
        if global.direction == 1 sprite_index = spr_rose_jumpR; // up-right
        else sprite_index = spr_rose_jumpL; // up-left
    }
    else // falling DOWN
    {
        if global.direction = 1 sprite_index = spr_rose_fallR; // down-right
        else sprite_index = spr_rose_fallL; // down-left
    }
}
if vsp<=0 && (!key_jump_held) vsp = max(vsp,0)
// COLLIDE WITH WALLS (HORIZONTALLY)
if (place_meeting (x+hsp, y, obj_ground))
{
    while(!place_meeting(x+sign(hsp), y, obj_ground))
    {
        x += sign(hsp);
    }
    hsp = 0;
}

// COLLIDE WITH WALLS (VERTICALLY)
if (place_meeting(x, y+vsp, obj_ground))
{
    while(!place_meeting(x, y+sign(vsp), obj_ground))
    {
        y += sign(vsp);
    }
    vsp = 0;
}

// MOVE CHARACTER
x += hsp;
y += vsp;
EDIT: Added 1 pixel to the bottom bounding box of each sprite and now it seems to be fixed, but she looks like she's floating in the air. Any middle ground solutions?
 
Last edited by a moderator:
N

Nirwanda

Guest
All the bounding boxes used to be 119 pixels at the bottom when it happened now they are 120 and it doesn't happen anymore, but sprite seems to be floating. My theory is that the jumping code makes her stop at a fractional height, say 7.5 and that makes her get stuck in the ground. But since at 120 she's floating the sprite doesn't collide and that's why I can't reproduce it anymore. Could it be that? Is there any way to round up the jumping height at which it stops?

EDIT: setting Y=round(y + vsp); seems to have solved the issue
 
Last edited by a moderator:
Top