Trying to get a special jump behavior

J

jwrose

Guest
Hello!
First time posting- I'm working on my first game in GameMaker after doing tutorials. I'm trying to make a somewhat unique jump behavior where a tap does a fairly normal gravity based jump and holding makes you go up a bit more (like a double jump) but then I want the y value to remain constant so the jump "levels-off" and the character basically hovers in place for a few seconds before dropping. I have the basics working but I can't get this hover effect to work. I best I've gotten is a double jump. Below is the related code. Any help would be appreciated. I've done some programming in the past, but I'm not a programmer per se- so I'm sure I'm overlooking something that will be easy for a more experienced programmer.

button_press = keyboard_check_pressed(vk_space);
button_held = keyboard_check(vk_space);

//this decides if the button is simply pressed or held:

if(button_press)
{
button_timer = 0.1;
}

if(button_held)
{
button_timer += 0.1;
}

if(button_timer <= 1 and button_timer> 0)
{
button_state = "pressed";
}

if(button_timer > 1.1)
{
button_state = "held";
}

//Jump code for press, this works fine

if (button_press = true) && (place_meeting(x,y+vsp,oWall))
{
vsp = -18;
jetpack = 1;
}

//controls hover jump. This is the part that I'm having trouble with.
if (button_state = "held") && (jetpack == 1)
{
// I created a loop to try to control what happens during this "second jump". I've tried tons of stuff here. This is one version of what I've come up with.
var glassceiling = 0
for (glassceiling = 0; glassceiling < 1000; glassceiling += 1)
{
vsp = +vsp - grv;
button_timer = button_timer - 0.1;
}

if (button_timer <= 0.1)
//I'm using this just to return everything to defaults. I'm played around with all of these variables at one time or another, so they're mostly unnecessary, bit I left them for now.
{
button_held = false;
vsp = -17;
grv = 1.1;
walksp = 7;
jetpack = 0;
keyboard_key_release(vk_space);
}


}


Appreciate any help! Thanks everyone!
 
Ok so first thing is that for loop isn't helping. As I understand, for loops operate in one frame of your game, basically trying to do a 1000 iterations of your code there in one step.

So most of that looks pretty good to me. The way I might try and do it is by having an if statement that checks if you are at the peak of your jump. Not sure the best way to do that exactly.
Maybe something like this:

Code:
if(button_held && jetpack == 1)
{
     if(vsp > -1)
     {
           vsp = 0;
           grv = 0;
           button_timer -= 0.1;
     }
}
Don't just copy/paste that btw, I wrote it on my phone so there may be mistakes haha.

EDIT: Oh wait, I think I misunderstood your code.(Sorry, on my phone and it can be hard to read code not formatted in a codeblock).

EDIT2: So I think if you just remove that for loop and change the vsp and grv to 0, then it should work. Hope that helps.
 
Last edited:
J

jwrose

Guest
Thanks!
That fixed the immediate issue. My character got stuck in the air, but it was the right hover effect. I've got him coming back down now, too- I just need to squash some bugs. Thanks again!
 
J

jwrose

Guest
OK, so I thought I had it, then realized I made a big mistake. After the character jumped, hovered, I was having them come back down after traveling so far on the X axis. Unfortunately, this means they will float indefinitely if you don't make them move on the x axis. What I really need is a time based way to making them drop after X seconds. Trying to solve that issue (which I've been struggling with for awhile) has brought up a reoccurring problem that you may be able to explain to me. Below is my code. I decided to use a timer loop to control how long they remain in the air . The issue I keep running into with these timer loops is that ALL movement freezes while it ticks up to 50000. I was hoping the left and right control I had would remain (so the character would remain in the air moving left or right before falling back down once the timer completed), but instead I cannot control the character and they freeze in place until the timer completes. Is there something I'm overlooking here or is a timer loop just not a good solution for that? Thanks for any help!


if (button_state = "held") && (jetpack == 1)
{
timer1 = 0;

for (timer1 = 0; timer1 <50000; timer1 +=1)

if timer1 == 50000
{

vsp = vsp + grv;
grv = 1.1;
jetpack = 0;
}
else
{
vsp = 0;
grv = 0;
}

}
 
Last edited by a moderator:
Haha ok, so. For loops will always execute in one step so you should never use them for timers. Either use alarms or just increment the variable yourself like you did with the button_held.
 
J

jwrose

Guest
OK- I see. I totally misunderstood for loops. So I was essentially extending out a single frame for longer periods of time. Thank you for pointing me in the right direction. I've been struggling with getting this jump system working for over a month (on-and-off very infrequently) and now I got it working in the last two days with your help.

I had avoided alarms so far because the documentation was not very clear and most posts about them I found were lacking on examples of how you actually set them up. I finally found a good resource on alarms and get it working. https://www.reddit.com/r/gamemaker/comments/26e94o/can_someone_please_explain_to_me_how_to_use/

Anyway, thanks again!
 
Last edited by a moderator:
Glad to hear it! Alarms can be pretty useful. They are pretty simple but I still make mistakes with them all the time. Once you understand how they work, they are a great asset.
Good luck with your game, dude.
 
Top