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

Platformer trampolines and holding the jump key

D

Dibidoolandas

Guest
I'm having a really difficult time with adding trampolines and I'm not sure why. Sadly explaining it isn't going to be super easy but I'll do my best. Basically, I have code that tells me player to not jump as high if you're not holding the jump key in his "air" state. Here it is:

Code:
if ((vsp < 0) && (!key_jump_held)) //If we're moving up and not holding the jump button
    {
        vsp = max(vsp,-spd_jump/2); //Cut our jump speed in half
    }
Now here's the thing... I like this code, but I just want my trampoline jump to have a flat value that it pushes the player upward. So here's my code for landing on a trampoline, triggered from the trampoline object.

Code:
if (instance_place(x,y-1,obj_player))
{
    obj_player.bounce = true;
    alarm[0] = room_speed/5;
}

if (obj_player.bounce = true)
{
    obj_player.vsp -= obj_player.spd_jump;
}
Generally when you hit the jump key, it just runs that vsp -= spd_jump code once. But in this case, the trampoline tells it to run that code until the alarm turns it off at room_speed/5. So we end up jumping higher.

The problem comes in when I try to turn off that first code I posted. My first (and really only) though for how to do this was to just create a conditional like so.

Code:
if (bounce = false)
{
    if ((vsp < 0) && (!key_jump_held)) //If we're moving up and not holding the jump button
    {
        vsp = max(vsp,-spd_jump/2); //Cut our jump speed in half
    }
}
For reasons I simply cannot wrap my head around, this doesn't work. I have no idea why, but if I don't hold the jump key while bouncing on the trampoline object, it cuts my jump speed in half still.

I did a test where I wrote the following instead of if bounce = false:

Code:
if !((x > obj_spring.x - 35) && (x < obj_spring.x + 35))
This is hacky as Hell, but basically if my player is within 70 pixels to the left or right of the trampoline (obj_spring), it turns off my short jump. So why in the world is bounce = false not doing it?

It's probably glaringly obvious, but it's driving me nuts that I can't get this. I really don't want to have to create a new state just to handle trampolines. Any help would be greatly appreciated. Thanks.
 
With your code, for 1/5 of a second, you should have a low vsp from the trampoline and as soon as you are past the 1/5 of a second, it will cut the speed to half a jump unless the jump key is held. It's not an error of your code but an error on what you think your code should be doing. An easy fix would be setting a variable holding the jump state and only cutting your jump if that jump state says you are in fact jumping.
Code:
isJumping = false;

if( onGround and jumpPressed ){
   isJumping = true;
   vsp = -jumpSpeed;
}

if( isJumping and vsp>=0 ){
   isJumping = false;
}

if( isJumping ){
   //code for variable jump here
}
Bouncing on the trampoline shouldn't trigger the isJumping state.
 
D

Dibidoolandas

Guest
With your code, for 1/5 of a second, you should have a low vsp from the trampoline and as soon as you are past the 1/5 of a second, it will cut the speed to half a jump unless the jump key is held. It's not an error of your code but an error on what you think your code should be doing. An easy fix would be setting a variable holding the jump state and only cutting your jump if that jump state says you are in fact jumping.
Code:
isJumping = false;

if( onGround and jumpPressed ){
   isJumping = true;
   vsp = -jumpSpeed;
}

if( isJumping and vsp>=0 ){
   isJumping = false;
}

if( isJumping ){
   //code for variable jump here
}
Bouncing on the trampoline shouldn't trigger the isJumping state.
Ahhh I think I finally see. The higher jump code is running AFTER the bounce has been turned off.

I wanted to get away with this without adding another variable, but I think you're right, this is the easiest way to accomplish this. And this will actually give me more control in other areas (a similar thing happens when I transition out of a swimming state, but I should be able to control that now). Thanks for taking a look, I'll test this tonight.
 

M. Idrees

Member
Check by replacing to this code:
Code:
if (instance_place(x,y-1,obj_player) && !obj_player.bounce)
{
    obj_player.bounce = true;
    alarm[0] = room_speed/5;
}
 
C

ConsolCWBY

Guest
I don't mean to be pedantic, but in this code:
Code:
if (instance_place(x,y-1,obj_player))
{
    obj_player.bounce = true;
    alarm[0] = room_speed/5;
}

if (obj_player.bounce = true)
{
    obj_player.vsp -= obj_player.spd_jump;
}
Don't you want to set a conditional, NOT AN ASSIGNMENT??! :: "if (obj_player.bounce == true)",
A:
The only thing squishy is my butt! Code should NOT be squishy even if the compiler is! :p
 

M. Idrees

Member
I don't mean to be pedantic, but in this code:

if (instance_place(x,y-1,obj_player))
{
obj_player.bounce = true;
alarm[0] = room_speed/5;
}


if (obj_player.bounce = true)
{
obj_player.vsp -= obj_player.spd_jump;
}

Don't you want to set a conditional, NOT AN ASSIGNMENT??! :: "if (obj_player.bounce == true)",
A:
The only thing squishy is my butt! Code should NOT be squishy even if the compiler is! :p
yes Replace to red colored code above
The obj_player.bounce == true will let the alarm to reach 0
Otherwise the alarm will reset Every step if your bounce object colliding with the player object.
 
D

Dibidoolandas

Guest
I don't mean to be pedantic, but in this code:
Code:
if (instance_place(x,y-1,obj_player))
{
    obj_player.bounce = true;
    alarm[0] = room_speed/5;
}

if (obj_player.bounce = true)
{
    obj_player.vsp -= obj_player.spd_jump;
}
Don't you want to set a conditional, NOT AN ASSIGNMENT??! :: "if (obj_player.bounce == true)",
A:
The only thing squishy is my butt! Code should NOT be squishy even if the compiler is! :p
Here's the thing: I'm bad at code. I'm decent at figuring out how to hack my way through 💩💩💩💩, but one of my programmer friends was looking at this the other day and confused about why I was using single equals instead of double. I kinda just shrugged. I basically use them interchangeably and haven't noticed an issue anywhere in my code. I've been meaning to go through and clean up things here and there, but I assumed GML didn't make a differentiation, whereas something like Javascript would set those conditions to true if I used a single equals (at least as far as he explained it to me). Is it actually functioning differently with the single quotes, or are you more just wanting it to be double because it's more proper syntax?
 

M. Idrees

Member
Single equal sign or double equal sign not making any difference in GML when using in if statment as a condition.
This is not the problem.The problem is with your alarm that you set in the [ Step Event ].
You know that [ Step Event ] is a loop which reads all the code 30 times in a sec. Reading speed depends on the room_speed if it is 30 [Step Event] will loop 30 time in a sec or if room_speed is 60 [ Step_Event ] will read 60 time per second All the code that is in the [ Step Event ].
Code:
if (instance_place(x,y-1,obj_player))
{
    obj_player.bounce = true;
    alarm[0] = room_speed/5;
}
So in the above code if the player object in on the spring obj or bouncy object the [ Alarm[0] ] will not reach to 0 until the player object is not on the bouncy object because the above code will check the collision 30 steps or 60 steps every sec.
So Make your code like this
Code:
if (instance_place(x,y-1,obj_player) && !obj_player.bounce)
{
    obj_player.vsp -= obj_player.spd_jump;
    alarm[0] = room_speed/5;
    obj_player.bounce = true;
}
In the above code you set another condition :- if ( instance_place(x,y-1,obj_player) && !obj_player.bounce )
This code will only work if the player is on the bouncy object and also the player is not ready to bounce
If these 2 conditions are true then the [ Step Event ] will read the code that is in the if statement between { }.
Now if the player is on the bouncy object it will check the !obj_player.bounce if it is false it will set to true and then when the [ Step Event ] again come to read this code again it will check the conditions of if statement. Now the conditions are changes because we changed the obj.player.bounce to true.
So now the [ Step Event ] will not the code that is in the if statement and will let the [ Alarm[0] Event ] to work.
 
D

Dibidoolandas

Guest
Single equal sign or double equal sign not making any difference in GML when using in if statment as a condition.
This is not the problem.The problem is with your alarm that you set in the [ Step Event ].
You know that [ Step Event ] is a loop which reads all the code 30 times in a sec. Reading speed depends on the room_speed if it is 30 [Step Event] will loop 30 time in a sec or if room_speed is 60 [ Step_Event ] will read 60 time per second All the code that is in the [ Step Event ].
Code:
if (instance_place(x,y-1,obj_player))
{
    obj_player.bounce = true;
    alarm[0] = room_speed/5;
}
So in the above code if the player object in on the spring obj or bouncy object the [ Alarm[0] ] will not reach to 0 until the player object is not on the bouncy object because the above code will check the collision 30 steps or 60 steps every sec.
So Make your code like this
Code:
if (instance_place(x,y-1,obj_player) && !obj_player.bounce)
{
    obj_player.vsp -= obj_player.spd_jump;
    alarm[0] = room_speed/5;
    obj_player.bounce = true;
}
In the above code you set another condition :- if ( instance_place(x,y-1,obj_player) && !obj_player.bounce )
This code will only work if the player is on the bouncy object and also the player is not ready to bounce
If these 2 conditions are true then the [ Step Event ] will read the code that is in the if statement between { }.
Now if the player is on the bouncy object it will check the !obj_player.bounce if it is false it will set to true and then when the [ Step Event ] again come to read this code again it will check the conditions of if statement. Now the conditions are changes because we changed the obj.player.bounce to true.
So now the [ Step Event ] will not the code that is in the if statement and will let the [ Alarm[0] Event ] to work.
Thanks for the info. I'm going to dig through this in the morning. I implemented strawbry_jam's recommendation and it worked, but I'd like to see if this does the trick as well for posterity.

Also good to know that there's not really a difference in GML with the double equals. I may go through all my code anyways to check and make sure I'm using the proper syntax. You know just to be a weirdo.
 
C

ConsolCWBY

Guest
Single equal sign or double equal sign not making any difference in GML when using in if statment as a condition.
This may change in the future if they ever tighten up the semantics. Best coding practices will avoid this. ;)
 
Top