GameMaker: Studio

GameMaker: Studio

View Stats:
tj May 28, 2018 @ 10:40pm
Jumping using code
i got this code in the step event of the player
//base vspeed
vspeed = 4
//walking
if keyboard_check(vk_left){if hspeed > -4 {hspeed = -4}}
if keyboard_check(vk_right){if hspeed < 4 {hspeed = 4}}

//collision + gravity <- i am proud of this part; it took me a long time to figure it out
if !place_empty(x, y+vspeed)
{
move_contact_solid(direction, 5);
vspeed = 0;
}

if !place_empty(x+hspeed, y)
{
move_contact_solid(direction, 5);
hspeed = 0;
}

//jumping
if keyboard_check_pressed(vk_up) and !place_empty(x,y+1)
{
vspeed = -40;
}

but whenever i press up the player jump really fast and doesn't get any height at all
Last edited by tj; May 28, 2018 @ 11:00pm
< >
Showing 1-2 of 2 comments
The Winter Bud May 29, 2018 @ 3:07am 
//base vspeed (A) vspeed = 4 //collision + gravity <- i am proud of this part; it took me a long time to figure it out (B) if !place_empty(x, y+vspeed) { move_contact_solid(direction, 5); vspeed = 0; } //jumping (C) if keyboard_check_pressed(vk_up) and !place_empty(x,y+1) { vspeed = -40; }
These three together are the problem.

(A) change vspeed to moving downward. then(B) if there is not a solid 40 pixels below the player, move down(vspeed=4) until we contact with a solid using 5 pixels per check at a time. then(C) if nothing is below the player and the user is pressing up key, move player up 40 pixels.

Though it works in A,B,C order you have to watch for looping concepts. The step event ends with (C) but then begins again at (A) so in a sense you can get a C, next step, A, B order

if (C) you are standing on a solid, then press up, you go up 40 pixels. In the next step, the script changes(A) the vspeed back to 4 and starts(B) to find a solid to move against because nothing is below the player.

EDIT: a possible solution is to take out the vspeed=4 line and replace it with something like
gravity=4
. or submit your objects to the physics engine and let game maker handle the physics. You'll have to tweek the parameters of the physics objects. making solid block kinetic. And, taking care when you change the mass and bounciness of moving objects for they can get out of hand quick.

resources
article by YoYo on physics[www.yoyogames.com]
video by Shaun Spawlding on physics
video of basic physics with Heartbeast
video playlist on physics
Last edited by The Winter Bud; May 29, 2018 @ 10:06am
tj May 29, 2018 @ 11:21am 
i've changed it to: //base vspeed
if vspeed < 4 {vspeed += 1}
//walking
if keyboard_check(vk_left){if hspeed > -4 {hspeed = -4}}
if keyboard_check(vk_right){if hspeed < 4 {hspeed = 4}}

//collision + gravity <- i am proud of this part; it took me a long time to figure it out
if !place_empty(x, y+vspeed)
{
move_contact_solid(direction, 5);
vspeed = 0;
}

if !place_empty(x+hspeed, y)
{
move_contact_solid(direction, 5);
hspeed = 0;
}

//jumping
if keyboard_check_pressed(vk_up) and !place_empty(x,y+1)
{
vspeed = -16;
} //i'll tinker more with the numbers tomorrow
and it now works
< >
Showing 1-2 of 2 comments
Per page: 1530 50

Date Posted: May 28, 2018 @ 10:40pm
Posts: 2