if !place_meeting(x,y+1,obj_block) // if we are in the air
{
grav = .5; // set the gravity to .5
vspd += grav; // add .5 to our vspd once every step
}
else // else if we are on the ground
{
grav = 0; // set the gravity to 0
vspd = 0; // set vspd to 0 to stop moving
if keyboard_check_pressed(vk_up) // since we are on the ground, we can handle jumping here, so check if we pressed up
{
vspd = -10; // set the vspd to -8, which will make us jump
}
}
if keyboard_check_released(vk_up) // if we released the up button while in the in air
{
vspd *= .5; // divide our vspd by 2, creating a smooth type of variable jumping
}
if vspd > 10 // we don't want to fall too fast, so lets limit our vspd
{
vspd = 10; // note that if you want to be able to fall fast, you can remove this without affecting the code (but I wouldn't)
}
repeat(abs(vspd)) // we want to check for a collision every pixel, so we use a repeat() function to check every pixel while falling
{
if !place_meeting(x,y+sign(vspd),obj_block) // vspd can be negative or positive, so we check 1 pixel above or below, depending on which direction we are going
{
y += sign(vspd); // add to our y value 1 pixel at a time, letting use check for a collision every pixel
}
else // else if we hit a block
{
vspd = 0; // stop moving vertically
}
}
hspd = (keyboard_check(vk_right)-keyboard_check(vk_left))*5; // a nice little trick that will set our hspd to +4 or -4 depending what button we are holding
repeat(abs(hspd)) // same as the vspd, we want to check for a collision at every pixel we move
{
if !place_meeting(x+sign(hspd),y,obj_block) // if there is no block to our left or right
{
x += sign(hspd); // add to our x value depending on which way we are going
}
}(note: this is not my code, all credit goes to Canite)I mostly figured out how to invert it by simply changing positive values to negative values and vice versa, but the only problem is the jumping. I try to jump but it's kinda glitchy, so how would this code be altered to allow jumping with inverted gravity? Thank you.
Edited by Tom Fama, 04 February 2012 - 06:20 PM.











