• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

GMS 2.3+ [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