Legacy GM {CLOSED}Auto jump mechanic problems

Doc

Member
Hello all! first off, I'll apologize in advance as this very likely has been covered before, but I am two days in and have a super limited understanding of GML and programming in general.

So I have a working state machine up and running. obj horizontal movement is working fine, as well as a timer to move the obj into the next state:

Code:
script_execute(scr_player_move);

//if the jump timer triggers, reset it
//so it can trigger again as long as on the ground
if ((jumptimer <= 0) && vspeed == 0)
    {
    jumptimer = 70;
    }

//check and force collision to the ground
if (!place_meeting(x, y +1, obj_floor))
    {
    vspeed = 1;
    }
    else
    {
    vspeed = 0;
    }
 
//alarm jump
if (jumptimer && !--jumptimer)
    {
        state = states.air;
    }
My problem begins when I move into my "jumping state":
Code:
script_execute(scr_player_move);

//jump setting
for (--vspeed; position_empty(x, y + 1); vspeed = 2)
{
--vspeed;
}

//enemy collision 

/*
//ground collision returns to ground state
if (!place_meeting(x, y +2, obj_floor))
    {
    vspeed = 5;
    }
    //else if (!place_meeting(x, y +1, obj_floor))
        {
        state = states.ground;
        }
A lot of this has been commented out and changed due to frustration lol. Here is the obj create event code just in case:
Code:
//movement speed used for left right movement
spd = 5;

//jump and gravity speed
jmpspd = 5;
grvspd = 2;

// zero out object movement
hspeed = 0;
vspeed = 0;

//used to stop left and right movement
friction = 0.25;

//triggers return to air state from ground
jumptimer  = 20;

//sets initial player state
state = states.ground;

I've tried a few different IF statements, working with vspeed, gravity, and a combination of the two. The best I can manage is jerky hops of only a few pixels, or the obj flies/floats of the screen.

What I would like to have happen is this:
When the obj enters the jumping state, it immediately jumps (speed builds up gradually to the peak, then speeds build on the descent, but slower), then if obj hits the ground it moves back into the ground state. If it collides with another object, it would grab a new vspeed/gravity off of them and repeat.

Essentially the game I'm trying to make is built around jumping on enemies. I figured I would start with a simple concept in the hopes of having a realistic to finish game. I have a pretty big background in art, so I want to see if I can complete a small game top to bottom, which will also let me know if I like the process. This is day two, and except for this headache, I like it so far :)

Thanks in advance for any help/correction/guidance on this problem, and if any of you need an extra set of eyes on art related problems let me know.
 

samspade

Member
Generally, in a jump, speed does not build up to a peak. The object is fastest as it leaves the ground and slows until it hits 0 vertical speed and starts to move down. To image what it would look like to have an object increase in speed during a jump, think of a rocket lifting off.

I'm also a little surprised that your jump setting for loop doesn't get you stuck in an endless loop. Even if it doesn't, that is not a good way to use a for loop. I would actually re-work the whole system as it will be beneficial to get away from the default hspeed vspeed variables as well. They'll most likely cause problems down the road. Here's an example. There are also several good free character assets on the market place which might be worth looking through (one of which uses scripts for states rather than a switch statement which is my preferred method).

Code:
///create event


//movement variables
hsp        = 0;         
carry_hsp  = 0;         
walk_speed = 6;         
vsp        = 0;         
carry_vsp  = 0;         
jump_speed = -12;       
grav       = .5;       

//state variables
WALK    = 0;             
JUMP    = 1;     
state   = WALK;           


///step event


//inputs
var left  = keyboard_check(ord("A")) || keyboard_check(vk_left);
var right = keyboard_check(ord("D")) || keyboard_check(vk_right);
var up    = keyboard_check(ord("W")) || keyboard_check(vk_up);
var down  = keyboard_check(ord("S")) || keyboard_check(vk_down);
       
var jump    = keyboard_check_pressed(vk_space);
var action  = mouse_check_button_pressed(mb_left);

     
//states - WALK, JUMP
switch (state) {

    //walk state
    case WALK:                                     
        hsp = (right - left) * walk_speed;           
        vsp += grav;                               
   
        if (jump) {                                 
            state = JUMP;                           
            vsp = jump_speed;}                     

        if (!place_meeting(x, y + 1, obj_wall)) {
            state = JUMP;}
   
        break;

    //jump state
    case JUMP:                                     
        hsp = (right - left) * walk_speed;         
        vsp += grav;                               
   
        if (place_meeting(x, y + 1, obj_wall)) {
            state = WALK;}                         
       
        break;
   
}


//movement and collision
carry_hsp += hsp;
carry_vsp += vsp;
var hsp_new = round(carry_hsp);
var vsp_new = round(carry_vsp);
carry_hsp -= hsp_new;
carry_vsp -= vsp_new;

repeat(abs(hsp_new)) {
    if (!place_meeting(x + sign(hsp), y, obj_wall)) {
        x += sign(hsp);}
    else {
        hsp = 0;
        break;}}

repeat(abs(vsp_new)) {
    if (!place_meeting(x, y + sign(vsp), obj_wall)) {
        y += sign(vsp);}
    else {
        vsp = 0;
        break;}}
With this code, bouncing on enemies is very easy as you only have to add to the player's vsp (or set the player's vsp) when necessary. Note that this code would require some modification in order to work horizontally as it sets the player's horizontal movement each step.
 

Doc

Member
Sorry for the super late response, thanks for the info on this! I've actually decided I'm going to jump over to GMS2 what with the sale, and end of life stuff. Though that shouldn't change any of my problem, I'll have to convert everything over and see where I'm at. I think this got me pointed in the right direction though :)
 

samspade

Member
Sorry for the super late response, thanks for the info on this! I've actually decided I'm going to jump over to GMS2 what with the sale, and end of life stuff. Though that shouldn't change any of my problem, I'll have to convert everything over and see where I'm at. I think this got me pointed in the right direction though :)
No problem. GMS 2 is way better in my opinion. The UI alone makes it worth the switch.
 
Top