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

GameMaker [CLOSED]I don't know why place_free/empty is wrong, also movement

B

BGamers11

Guest
I was trying to make a basic code of collision and movement, but I don't know why the code don't works, I already tried many times, using place_empty, place_free, but nothing works. The problem is that the character keeps floating in air, and it don't stops walking when I release a key. (Notes: Sprite Size is 16x50, but looks like it already "detect" a collision when I put y + 15, I think it searches for the collision box, not it's origin, also it don't detects collision with y + a number below 15)(It has 2 events: Create(with a single line: "gravity = 1;" and the Step Event, posted below):


Code:
if (y > room_height) {
    x = 32;
    y = 640;
}

if (vspeed > 12) {
    vspeed = 12
}

if place_free(x, y + sprite_height) == false {
    gravity = 0 }
    else {
    gravity = 1
}

if keyboard_check(vk_right) and hspeed == 0 {
    hspeed = hspeed + 1 }
if keyboard_key_release(vk_right) and hspeed > 0  {
    hspeed = hspeed - 1
}

if keyboard_check(vk_left) and hspeed == 0 {
    hspeed = hspeed - 1 }
if keyboard_key_release(vk_left) and hspeed < 0 {
    hspeed = hspeed + 1
}
 

3dgeminis

Member
Try this way
Code:
if keyboard_check(vk_right) {hspeed+=1}
   
if keyboard_check(vk_left) {hspeed-=1}
   
if ( not keyboard_check(vk_right) and not keyboard_check(vk_left) )
  {
   if hspeed>0 hspeed-=1
   if hspeed<0 hspeed+=1
  }
  }
 
Last edited:
B

BGamers11

Guest
Try this way
Code:
if keyboard_check(vk_right) {hspeed+=1}
  
if keyboard_check(vk_left) {hspeed-=1}
  
if ( not keyboard_check(vk_right) and not keyboard_check(vk_left) )
  {
   if hspeed>0 hspeed-=1
   if hspeed<0 hspeed+=1
  }
  }
I can't test your script now, but I guess it will work for moving, but can you please help me discovering the problem with the gravity?
 

3dgeminis

Member
If you use the height of the sprite (50pixels) The object is above the floor 50 pixels, so it seems to be floating.

You'd have to put it this way:
Code:
if place_free(x,y+1) {gravity=1} 
else {gravity=0}
-If there is no solid 1 pixel below, the severity is 1.
-If there is, gravity is 0.

But even if we stop gravity, speed still acts. To stop it in the collision event with the solid object:
Code:
move_contact_solid(270,vspeed)
vspeed=0
-What it does is move the player down while there is space between the player and the solid.
-Then we stop the speed.
 
Top