• 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!

Legacy GM [RESOLVED!]Problems with multiple movement inputs

Lemmy4K

Member
Let me get straight to the point. In my 2D side scroller my player doesn't react properly when I do certain inputs. For example, let's say I'm holding the right arrow so my player goes right. While holding right I push the left arrow as well. So I'm holding left AND right. Then I released right and just hold left. If I do this action my character continues to move right when I want it to go left. And if I do the same thing but vise versa, I go left instead.

For visual learners (Gif for the inputs):

https://piskel-imgstore-b.appspot.com/img/9b42f191-83a0-11eb-9c22-9bef4b5e0036.gif

Here's the code for my player.
GML:
Create Event
///Variables
grav = 1;
hsp = 0;
vsp = 0;
jumpspeed = 11;
movespeed = .4;
max_hsp = 5;


Here's the code for my player.
Step Event
///Movement, Jumping, Acceleration, Collision

///Get the Player's Input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_up);

//Reacts to the player's inputs
move = key_left + key_right;
if (key_left = -1) previous_dir = -1
if (key_right = 1) previous_dir = 1

//Acceleration/Momentum
if (hsp < max_hsp) && (hsp > -max_hsp)
{
    hsp += move * movespeed;
}

else if (hsp = max_hsp)
{
    if (key_right)
    {

        hsp = max_hsp;
    }
    else
    {
        hsp -= 1;
       
    }
}
else if (hsp = -max_hsp)
{
    if (key_left)
    {
        hsp = -max_hsp;
    }
    else
    {
        hsp += 1;
    }
}
if (hsp > 0) && (key_left = 0) && (key_right = 0) && (place_meeting(x,y+1,Ground)) {hsp -= .45}

if (hsp < 0) && (key_left = 0) && (key_right = 0) && (place_meeting(x,y+1,Ground)) {hsp += .45}

//Gravity/Jump
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,Ground))
{
    vsp = key_jump * -jumpspeed
}

//Horizontal Collision
if (place_meeting(x+hsp,y,Ground))
{
while(!place_meeting(x+sign(hsp),y,Ground))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;

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

y += vsp;

I believe the problem is where it says "Acceleration/Momentum"

Just thought I'd throw in the entire script.
 

Mk.2

Member
These lines:

GML:
else if (hsp = max_hsp)

else if (hsp = -max_hsp)
Should be:

GML:
else if (hsp >= max_hsp)

else if (hsp <= -max_hsp)
Since it's possible that hsp doesn't equal the same exact value as max_hsp.

Also this:

GML:
if (key_left)
{
    hsp = -max_hsp;
}
Should be:

GML:
if (-key_left) //Or (key_left == -1)
{
    hsp = -max_hsp;
}
Since key_left was defined as -keyboard_check(vk_left), not keyboard_check(vk_left).
 
Last edited:

Lemmy4K

Member
These lines:

GML:
else if (hsp = max_hsp)

else if (hsp = -max_hsp)
Should be:

GML:
else if (hsp >= max_hsp)

else if (hsp <= -max_hsp)
Since it's possible that hsp doesn't equal the same exact value as max_hsp.

Also this:

GML:
if (key_left)
{
    hsp = -max_hsp;
}
Should be:

GML:
if (-key_left) //Or (key_left == -1)
{
    hsp = -max_hsp;
}
Since key_left was defined as -keyboard_check(vk_left), not keyboard_check(vk_left).
Yea this worked out great! Thanks man for your help! : D
 
Top