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

[Solved] Jump with UP + Left or Right keys

Traversal

Member
I just restarted working with GMS2 and came across my first problem pretty fast :/

Below is my code for the player movement.
I want my object/player to move left, right and jump.

It works so far, but I am unable to code it the way that the player can jump
diagonal while keep pressing VK_LEFT (or right) and then hitting VK_UP once...


GML:
if !place_meeting(x,y+1,obj_baseground)
{
    gravity = 2.5;
    grounded = 0;
}else
{
    gravity = 0;
    grounded = 1;
}


if keyboard_check(vk_right) //move right
{
    speed=5; image_speed=3;
    if(grounded == 1)
    {
        sprite_index =Vik1Run;
        }
        direction=0;
}else if keyboard_check(vk_left) //move left
{
    speed=5;
    image_speed=3;
    if(grounded == 1)
    {
        sprite_index =Vik1Run;
        }
        direction=180;
}else if (keyboard_check(vk_up) && grounded == 1) //jump only when grounded
{
    grounded =0;
    sprite_index = Vik1Jump; speed=255; image_speed=3; direction=90; // direction should have done the trick??
}


else { sprite_index =Vik1Stand; speed=0;  image_speed = 3;}
 

Slyddar

Member
Nesting if else statements like you have only allows one choice to be made. Since left and right are exclusive, they should be in an if else, but jumping is something you want to happen as well as the left and right movement, so just remove it from the else if nest.
 
Top