Legacy GM [SOLVED]Player Able to Flip Gravity Twice Instead of Once

S

Storm1208

Guest
Hey all! As a sort of pet project I've been attempting to recreate the game engine for the popular indie game VVVVVV within GMS 1.4. Everything has been going smoothly so far but I've had an issue for a few weeks now and I just can't seem to figure out how to resolve it.

Within my game, the player can click the space bar to flip gravity; however, the player shouldn't be able to flip gravity again until they make contact with the ground (or ceiling depending on their orientation :D) For some odd reason, my code is allowing the main character to "double flip"(in other words, after flipping once, the player can flip again even if they haven't made contact with the ground yet) Below is my code that both stops the flipping from being active and allows it to be used again.

Initial Gravity Flipping:
Code:
//Jumping
if(key_space && flip == 1)
{   
    flip = 0;
    image_yscale = -image_yscale;
    grv = -grv; //flip gravity if key pressed and on ground
}
Resetting Flip Variable and Detecting Ground Collision:
Code:
//Vertical Collision
if(place_meeting(x,y+vsp,obj_ground)) //if we're about to collide with a ground tile
{
    while(!place_meeting(x,y+sign(vsp),obj_ground))//keep moving until...
    {
        y += sign(vsp);
    }
    vsp = 0; //BAM - stop moving
    flip = 1;
}
I tried running my code through GameMaker's debugger and it looks like the flip variable is being reset to 1 before it's even reached within the if statement and I'm not entirely sure why(in the debugger, flip had already been set back to 1 while the program was still in the vertical collision's while loop). I'd appreciate any help!
 

ktiix

Member
When does flip get reset back to 1? Is it a single frame after setting it to 0? Or even the same frame? Or does it persist as 0 for a while and then revert?
In your step event, you can use show_debug_message (string(flip)); to print to the console every frame of the game to narrow down where it gets incorrectly set back to 1.
 
S

Storm1208

Guest
When does flip get reset back to 1? Is it a single frame after setting it to 0? Or even the same frame? Or does it persist as 0 for a while and then revert?
In your step event, you can use show_debug_message (string(flip)); to print to the console every frame of the game to narrow down where it gets incorrectly set back to 1.
Wow, thanks for the debug message tip! I actually didn't even know GameMaker had that!

Printing the flip value to the console showed something rather interesting. Despite my flip code, the initial flip doesn't actually set the flip variable to 0. The flip variable remains one on the first flip and doesn't change to 0 until the second flip (aka flipping again before making contact with the ground).

Based on this, something in my code is preventing the flip variable from changing to 0 during the first flip, but is successfully changing it to 0 on the second flip, which is odd.
 

woods

Member
your flip code tells us what to do while flipped...
how about adding another variable.. can_flip = 0 (while unable to flip)-changes to 1 when contact with new floor is made
 

Danei

Member
When does gravity get applied to vsp? If vsp is still positive due to pre-flip gravity on the frame when you flip, your collision code will trigger and set flip back to 1 on the same frame. Try setting vsp = -vsp inside the flip code to make sure that's not the issue.
 
S

Storm1208

Guest
When does gravity get applied to vsp? If vsp is still positive due to pre-flip gravity on the frame when you flip, your collision code will trigger and set flip back to 1 on the same frame. Try setting vsp = -vsp inside the flip code to make sure that's not the issue.
Oh my goodness, thanks! That was the exact error that was causing the bug. Your explanation made a lot of sense and modifying the vsp value fixed my problem!

Thank you so much for the help everyone!
 
Top