Before we get to the nitty gritty though, I think we should address the huge issue staring us straight in the face, namely that our main character is able to jump in the air infinitely. Let's Review:
if keyboard_check_pressed(vk_up)
{
vspeed = -27
}
It's a pretty basic command, with a pretty simple solution to our problem. If we don't want him to jump in the air, then we just have to make sure he's standing on the ground.
if keyboard_check_pressed(vk_up)
{
if place_meeting(x,y+1,obj_floor)
{
vspeed = -27
}
}
Yup, just add a second if statement checking if the player is touching the ground. There is another way to write this, using the "AND" statement. Doing so saves space, but I wrote it this way for something later. For now, we need to deal with another big problem. Right before landing, the character jitters to the ground.
if place_meeting(x,y+vspeed,obj_floor)
{
vspeed = 0
}
else
{
vspeed += 2
}
If you want to fix the problem, you need to figure out what exactly is causing it. In this case, just think it through frame by frame:
x = distance from floor |
The first is actually pretty easy to fix, thanks to a really handy command built into Game Maker:
move_contact_solid(dir,maxdist)
I love this command. Basically, it moves the object right up against the nearest solid in the direction you type. For the direction, you have to type a number, as in degrees. So for us, we need down, which is 270 degrees (if you haven't done much geometry, just remember 0 is east, 90 is north, 180 is west, and 270 is south). The second argument it wants is maxdist, which in this case, we just use the vspeed variable.
if place_meeting(x,y+vspeed,obj_floor)
{
move_contact_solid(270,vspeed)
vspeed = 0
}
else
{
vspeed += 2
}
Doing this fixes the entire problem. This character no longer jitters right before touches the ground. I'm sure this perfect programming won't come back to bite me in the butt later.
Foreshadowing!
Anyway, let's take a step back and finally give our walls some kind of purpose. We won't worry about "move_contact_solid()" in this case, if only because it's not noticeable at all.
if keyboard_check(vk_right)
{
if not place_meeting(x+8,y,obj_wall)
{
x += 8
}
}
else
{
if keyboard_check(vk_left)
{
if not place_meeting(x-8,y,obj_wall)
{
x -= 8
}
}
}
Okay, so this part looks like a horrible mess of brackets and if statements. This is only a small sign of how crowded the if, then's can become, so learning to read this is important. Just remember when typing these, to do like I do and line up corresponding brackets together (or something else if you format code differently than me) so you can keep track of everything.
Well, there we go. Now we've got the bare basics of mobility covered. Next though, we give our character the ability to double jump ONCE, and let the player control the strength of the jump.
EDIT:
This is the Game Maker file:
https://www.dropbox.com/s/shkliu9jnvzfw13/4%20Polishing%20Up%20Gravity.gm81
This is the executable:
https://www.dropbox.com/s/a47hichwe2z0lup/4%20Polishing%20Up%20Gravity.exe
No comments:
Post a Comment