So, now that the right and left navigation of my character are working with GML, I'm trying to implement the jump and collisions system and obviously it didn't work and once again I need your help to figure it out.
what I want : I just want my character to jump if you press up arrow key. To fall if there is nothing underneath him and to collide with solid blocks.
What happens : The jump is working but when the character is landing, he doesn't stop on the top of the block but somewhere in the middle of the solid block. My character can go through block on his left and right but if I stop him at the same place as a block, he get stuck on it. The landing is working but have the same problem as the jump for the landing.
So, this is my code. Let me know if you don't understand something. As always, I don't want you to make the code for me, I want to understand what happens and how to make it work.
Thank you
create event :
//Variables hspeed = 0 vspeed = 0 gravity = 0 onGround = true // false = Character jumping or falling ; true = Character on the groundstep event :
//**Collisions**
if !place_free(x,y+1) // check if the character is on the ground
{
onGround = true;
}
if place_free(x,y+1) // check if the character isn't on the ground
{
onGround = false;
}
if onGround = true // if the character is on the ground, all right, let him where he is
{
gravity = 0;
vspeed = 0;
}
if onGround = false //if there character isn't on the ground, bring him down
{
gravity += 0.2;
move_contact_solid(270,1); // move down until you collide with something solid
}
//**Jump**
if keyboard_check(vk_up) and onGround = true
{
vspeed -= 6;
sprite_index = MainGuy_Jump;
onGround = false;
}
//**Run to the right**
if keyboard_check(vk_right) and place_free(x+1,y) // move to the right only if there is free space at x+1 and you press right arrow
{
hspeed += 1
sprite_index = MainGuy_Run_Right;
image_speed = 0.5 ;
}
if hspeed > 5
{
hspeed = 5;
}
if (keyboard_check(vk_nokey))
{
hspeed = 0;
sprite_index = MainGuy_Stand ;
image_speed = 0.3;
}
//**Run to the left**
if keyboard_check(vk_left) and place_free(x-1,y) // move to the left only if there is free space at x-1 and you press left arrow
{
hspeed -= 1
sprite_index = MainGuy_Run_Left ;
image_speed = 0.5 ;
}
if hspeed < -5
{
hspeed = -5;
}
if (keyboard_check(vk_nokey))
{
hspeed = 0;
sprite_index = MainGuy_Stand ;
image_speed = 0.3;
}
Edited by Tartine, 12 May 2012 - 08:27 PM.











