Legacy GM Running in Platformer

N

NoFontNL

Guest
Hey all! I've a small problem when running my platformer game. If I press shift my character needs to run, but it only runs to the right. When I press shift + left arrow key, the player walks to the left and not runs to the left. This is the Run code:

Step Event Player:
Code:
key_left = -(keyboard_check(vk_left));
key_right = (keyboard_check(vk_right));
is_moving = (key_left) || (key_right);
key_jump = (keyboard_check(vk_space));
key_run = (keyboard_check(vk_shift));

move = key_left + key_right;
hsp = move * moveSpeed;

if (key_left = -1){
    image_xscale = -4; // Flips player if moving to the left.
}
if (key_right = 1){
    image_xscale = 4;
}

if (key_run) && (moveSpeed < 16) && (is_moving) { // If you' re pressing the run key (shift) and you're moving.
    moveSpeed += 0.5
    if (jumpSpeed < 17.5) {
        jumpSpeed += 0.5 // Acceleration: jumpSpeed gets higher with the time you're running.
    }
}


if !(key_run)  { // If you're not pressing the run key, slowly slow down.
    if moveSpeed > 5 {
        moveSpeed -= 1;
    }
    if jumpSpeed > 15 {
        jumpSpeed -= 1;
    }
}
So again, the problem is the player only runs to the right, not to the left.
 
A

Alex_Beach

Guest
Below the: if(key_run &&...) line, shouldn't moveSpeed+= .5*move? This would consider running in both directions
 
You are setting key_left to a negative value. In Gml, negative values are considered false when treated as booleans. This means your is_moving variable will be set to false when moving left, which then prevents your run code from being executed because of the usage of is_moving in the if() statement.
 
A

Alex_Beach

Guest
You are setting key_left to a negative value. In Gml, negative values are considered false when treated as booleans. This means your is_moving variable will be set to false when moving left, which then prevents your run code from being executed because of the usage of is_moving in the if() statement.
That's not the problem, when he checks for that key press, he puts a negative outside the parantheses. This means his 1s will be negative 1s. No error there. It's when he implements his shift key "run"
 
His run code will never execute because as I said, is_moving will be false.

Your suggestion is incorrect because the code is already multiplying move by moveSpeed and assigning it to hsp.
 
N

NoFontNL

Guest
That's not the problem, when he checks for that key press, he puts a negative outside the parantheses. This means his 1s will be negative 1s. No error there. It's when he implements his shift key "run"
Thanks for your replies, sadly it didn't work putting in 0.5*move.
To make sure, in the Create event of the player:
Code:
image_speed = 0;
image_index = 0;

//initialize variables:
hsp = 0;
vsp = 0;
grav = 1;
jumpSpeed = 15;
moveSpeed = 5;
grounded = 0;
I'll send the full code of the step event, maybe that would work:
Code:
//Get key input:
key_left = -(keyboard_check(vk_left)) // This negative value is not meant to be a boolean. It's an integer, because: key_left + key_right will result in 0 (do nothing), key_left will result -1 (Move to left) and key_right will result in 1 (Move to right)
key_right = (keyboard_check(vk_right))
is_moving = (key_left) || (key_right) // If you press key_left OR key_right
key_jump = (keyboard_check(vk_space))
key_run = (keyboard_check(vk_shift))

move = key_left + key_right; // Depends which direction you're about to go.
hsp = move * moveSpeed; // hsp will get set to move (-1 = left, 0 = nothing or 1 = right) * moveSpeed (5 =walking, 15.5 = running)

if (key_left = -1){ // Flip sprite if going to the left
    image_xscale = -4;
}
if (key_right = 1){
    image_xscale = 4;
}

if (key_run) && (moveSpeed < 16) && (is_moving) { 
    moveSpeed += 0.5 // I didn't put the *move after it because it didn't worked. And -1 * 0.5 will result in -0.5 so it subtracts it off the moveSpeed when doing += -0.5
    if (jumpSpeed < 17.5) {
        jumpSpeed += 0.5
    }
}


if !(key_run)  {
    if moveSpeed > 5 {
        moveSpeed -= 1;
    }
    if jumpSpeed > 15 {
        jumpSpeed -= 1;
    }
}

if (vsp < 10) {
    vsp += grav;
}

if (place_meeting(x,y+1,par_ground))
{
    vsp = key_jump * -jumpSpeed;
    grounded = 1;
}
else {
    grounded = 0;
}

if (key_jump) && (grounded) {
    audio_sound_gain(snd_jump,0.1,0)
    audio_sound_pitch(snd_jump,choose(0.8,0.9,1,1.1,1.2,))
    audio_play_sound(snd_jump,2,false)
}

if !(grounded) {
    image_index = 1;
}
else {
    image_index  = 0;
}


//Horizontal collison:
if place_meeting(x+hsp,y,par_ground) {
    while !(place_meeting(x+sign(hsp),y,par_ground))
    {
        x += sign(hsp);
    }
    hsp = 0
}
x += hsp;

//Vertical collison:
if place_meeting(x,y+vsp,par_ground) {
    while !(place_meeting(x,y+sign(vsp),par_ground))
    {
        y += sign(vsp);
    }
    vsp = 0
}
y += vsp;
That's the code.

Last note:
when replacing the line is_moving = ... with

is_moving = (keyboard_check(vk_left)) || (keyboard_check(vk_left))

instead of:

is_moving = (key_left) || (key_right)

then the character can run to the left, but not to the right.
 
A

Alex_Beach

Guest
You are setting key_left to a negative value. In Gml, negative values are considered false when treated as booleans. This means your is_moving variable will be set to false when moving left, which then prevents your run code from being executed because of the usage of is_moving in the if() statement.
You're right, I read your post wrong. Yes, to the OP, this is the issue
 
A

Alex_Beach

Guest
Is_moving=(-key_left)||(key_right).
Just add that negative sign before the key_left, and you should be good
 
N

NoFontNL

Guest
Nevermind, thank you both! I changed the
is_moving = (key_left) || (key_right)
To
is_moving = (keyboard_check(vk_left)) || (keyboard_check(vk_left))

But im checking here 2 tines for left. Thabk you again!
 
Top