Platformer - stuck in walls, freezes game

garlicsuter

Member
The jumping on platforms works well, until you hit the side of the next wall. The game freezes and you have to force quit. There is no animation in the sprite, so I've ruled that out. The collision mask is a rectangle.

I attached a gif of it happening.
 

Attachments

garlicsuter

Member
Do you have any collision code for us so we can help you?
Oops. I also added the wall, its parent, and the player create event image.
Code:
 //Get Player input
key_left = keyboard_check(vk_left); keyboard_check(ord("A"));
key_right = keyboard_check(vk_right); keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);

// Calculate Movement
var move = key_right - key_left

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,obj_wall_parent)) && (key_jump)
    {
            vsp = -7;
    }

// Horizantail Movement
if (place_meeting(x+hsp,y,obj_wall_parent))
{
            while (!place_meeting(sign(hsp),y,obj_wall_parent))
    {
        x = x = sign(hsp);
    }
    hsp = 0;
}
x = x + hsp;

// Vertical Movement
if (place_meeting(x,y+vsp,obj_wall_parent))
{
            while (!place_meeting(x,y+sign(vsp),obj_wall_parent))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;

//Doulbe Jump
ysp += grv;

if(keyboard_check_pressed(vk_space) && jump_current > 0)
    {
        ysp = -8
        jump_current--;
    }
    if(place_meeting(x, y + ysp,obj_wall_parent))
{
    while(!place_meeting(x, y + sign(ysp),obj_wall_parent))
    {
        y+= sign(ysp);
    }
    if(ysp > 0)
    {
        jump_current = jump_number;
    }
    ysp = 0;
}
y+= ysp;
 

Attachments

Simon Gust

Member
Oops. I also added the wall, its parent, and the player create event image.
Figured so, your code is pretty messy and the code order is not so optimal.
I rearanged and commented your code a bit
Code:
//Get Player input
key_left = keyboard_check(vk_left) || keyboard_check(ord("A"));
key_right = keyboard_check(vk_right) || keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);

// Calculate Movement
move = key_right - key_left;

// motion
hsp = move * walksp;

// gravity
vsp += grv;

// reset jump counter on ground
on_ground = place_meeting(x,y+1,obj_wall_parent);
if (on_ground)
{
    jump_current = jump_number;
}

// jumping
if (key_jump)
{
    if (on_ground)
    {
        // basic jump
        vsp = -7;
    }
    else
    if (jump_current > 0)
    {
        // double jump
        vsp = -8;
        jump_current--;
    }
}

// horizontal collision
if (place_meeting(x+hsp,y,obj_wall_parent))
{
    while (!place_meeting(sign(hsp),y,obj_wall_parent))
    {
        x += sign(hsp);
    }
    hsp = 0;
}
x += hsp;

// vertical collision
if (place_meeting(x,y+vsp,obj_wall_parent))
{
    while (!place_meeting(x,y+sign(vsp),obj_wall_parent))
    {
        y += sign(vsp);
    }
    vsp = 0;
}
y += vsp;
So the initial cause of your problem was the horizontal collision, the statement in the while loop is
Code:
x = x = sign(hsp);
This will set x to 1 if x is sign(hsp)
Once that is the case, an infinite loop starts (game freezing).
 
Last edited:

garlicsuter

Member
I replaced mine with your code, but now I can't jump at all.
Also, I think the
Code:
x = x = sign(hsp);
is supposed to be
Code:
x = x + sign(hsp);
right? I made that change too.

I'm combining different tutorials here, so that's why there is both vsp and ysp in the create event. Is it a bad idea to use both in one script?
Either way, I can't jump now. Thanks for your help with rearranging the code and using better variables.
 

Simon Gust

Member
I replaced mine with your code, but now I can't jump at all.
Also, I think the
Code:
x = x = sign(hsp);
is supposed to be
Code:
x = x + sign(hsp);
right? I made that change too.

I'm combining different tutorials here, so that's why there is both vsp and ysp in the create event. Is it a bad idea to use both in one script?
Either way, I can't jump now. Thanks for your help with rearranging the code and using better variables.
Yeh, lol I didn't even notice. But yes, never combine variables, always use one that does everything, like hsp doing all horizontal motion and vsp all vertical motion.
I edited my code to only use vsp instead of ysp.
 
Top