1. Hey! Guest! The 31st GMC Jam will take place between Nov 16th, 12:00 UTC (Friday noon) and Nov 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 [SOLVED] Adding double jump

Discussion in 'Programming' started by L1NDHOLM, Nov 8, 2018 at 9:51 PM.

  1. L1NDHOLM

    L1NDHOLM Member

    Joined:
    Oct 14, 2018
    Posts:
    16
    Hello, i have tried to add double jumping to my player object without success. I was hoping someone here could help me. Here is the code:

    Create Event
    Code:
    //Initialize Variables
    grav = 0.5; //gravitation
    gravmax = 10;
    hsp = 0;    //horizontal speed
    vsp = 0;    //vertical speed
    movespeed = 3;
    
    jumpspeed_normal = 6;
    jumpspeed_powerup = 10;
    
    jumpspeed = jumpspeed_normal
    
    
    //for ledge assistance
    grace_jump_time = 4; //number of grace period frames after leaving the ground you can still jump
    grace_timer = grace_jump_time;
    
    //for input buffering
    jump_buffer = 2; //number of grace period frames between player pressing jump and hitting the ground when they will still jump
    jump_buffer_timer = jump_buffer;
    Step Event
    Code:
    // Get the players input
    key_right = keyboard_check(vk_right);
    key_left = -keyboard_check(vk_left);
    key_jump = keyboard_check_pressed(vk_up); //original non-variable jumping
    key_jump_held = keyboard_check(vk_up); //for variable jumping
    
    is_on_ground = false; //for ledge assistance
    jump_is_inside_buffer = false; //for jump input buffering
    
    
    //Player sprite can change direction
    if (hsp > 0) { image_xscale = 1;}
    else if (hsp < 0) {image_xscale = -1;}
    
    //Bullet changes direction with player
    if (keyboard_check_pressed(vk_control))
        {
            inst = instance_create_layer(x, y, "Bullets", obj_bullet);
            inst.direction = (obj_player.image_xscale == 1) ? 0 : 180;
            inst.speed = 7;
        }
    
    //--------------------------------------------------------------------------------
    
    //React to inputs
    move = key_left + key_right;
    hsp = move * movespeed;
    if (vsp < gravmax) vsp+= grav;
    
    //For Ledge Assistance with input buffering
    if (place_meeting(x,y+1,obj_parentblock))
    {
        is_on_ground = true;
        grace_timer = grace_jump_time;
    }
    
    else
    {
        is_on_ground = false;
        grace_timer--;
    }
    
    //Jump Input Buffering
    if (key_jump)
    {
        jump_buffer_timer = jump_buffer;
    }
    
    
    if (jump_buffer_timer > 0)
    {
        jump_is_inside_buffer = true;
    }
    
    else
    {
        jump_is_inside_buffer = false;
    }
    
    //this is sort of a failsafe for when the buffer frames are set to 0, just use the old jump
    if (jump_buffer = 0)
    {
        jump_is_inside_buffer = key_jump;
    }
    
    if(jump_is_inside_buffer)
    {
        if (is_on_ground || grace_timer >0)
        {
            //vsp = key_jump * -jumpspeed;
            vsp = -jumpspeed;
            grace_timer = 0;
            jump_buffer_timer = 0;
        }
        jump_buffer_timer--;
    }
    
    //for variable jumping
    if(vsp <0 && !key_jump_held) vsp = max(vsp,0)
    
    
    //Horizontal Collision
    if (place_meeting(x+hsp, y, obj_parentblock))
    {
        while(!place_meeting(x+sign(hsp),y,obj_parentblock))
        {
            x+= sign(hsp);
        }
        hsp=0;
    }
    x += hsp;
    
    //Vertical Collision
    if (place_meeting(x, y+vsp, obj_parentblock))
    {
        while(!place_meeting(x,y+sign(vsp),obj_parentblock))
        {
            y+= sign(vsp);
        }
        vsp=0;
    }
    y += vsp;
    
     
  2. PlayerOne

    PlayerOne Member

    Joined:
    Mar 14, 2018
    Posts:
    217
    From reading your code what I'm not noticing is the number jumps being made when key_jump is pressed.

    This is something similar I use in my projects.

    Code:
    //CREATE:
    Jump_max=2; // <<< Max jumps that can be made.
    _jump=0; // <<< Number of jumps made.
    
    
    //STEP:
    if place_meeting(x,y+1,wall)
    {
    _jump=0; // <<< Reset jump to 0 when on a platform.
    }
    
    if (_jump<jump_max)
    {
                       if (key_jump)
                       {           
                       vsp = ((1) * -jump_force);
                       jumps++
                       }
    
    }
    
     
  3. PretzelBrosStudios

    PretzelBrosStudios Member

    Joined:
    Mar 26, 2017
    Posts:
    9
    I wouldn't use a buffer. Think of it more like Flappy Bird. Your character has to learn to fly first. I have removed the buffer, keep it if you need if for something else.
    Code:
    if (key_jump) {
        if (is_on_ground || grace_timer > 0) {
            vsp = -jumpspeed;
            grace_timer = 0;
        }
    }
    Your character should now be able to fly. Now we need to restrict unlimited jumping by adding a bool -> double_jump (remember to initialize it in the Create Event with double_jump = false).
    Code:
    if (key_jump && !double_jump) {
        if (is_on_ground || grace_timer > 0) {
            vsp = -jumpspeed;
            grace_timer = 0;
            jump_buffer_timer = 0;
        } else {
            vsp = -jumpspeed;
            double_jump = true;
        }
    }
    We have to reset the double_jump variable when your character hits the ground. Let's add a simple check at the beginning of your Step Event:
    Code:
    if (is_on_ground) {
        double_jump = false;
    }
    That should do the trick. I haven't tested the code. Hope it works.
     
  4. L1NDHOLM

    L1NDHOLM Member

    Joined:
    Oct 14, 2018
    Posts:
    16
    Thank you so much for the help PretzelBrosStudios, you really went out of your way and i appreciate it greatly!
    Now i will see if i can make the double jump into a premanent "power up".
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice