Pages

Friday, June 15, 2012

3: Rise and Fall

Well, now that the rectangle can move horizontally, it's time to tackle the signature aspect of platforming games, jumping. It only gets more complex from here, but we've got to get the basics down before we can tackle the good stuff.

Before we even touch the up key and jumping though, I want to tackle falling. After all, if you let the character jump, he'd continue on to infinity. You may have noticed that early on, I placed the player pretty high up. I did so to test gravity. Let's approach this just like we did last time, shall we? All of this code can go just below last post's code.

Gravity exists.


Er... well, sure, but what is gravity? A force that constantly pulls objects towards the ground is about as simplistic as I can put it. We're not programming realistic physics here, so all we need to do is code so that the player acts like gravity is affecting him.

vspeed += 2


There we go. Vspeed is a handy variable built into every object Game Maker creates. It adds the vspeed variable to the current y of the object every frame. Adding to the y means the object shifts downward, and as an added bonus, by constantly adding to the vspeed instead of just setting it to 2 or just adding to y manually, the character falls faster every frame. Problem solved, right?

Er...




Okay, so all we did was make the poor guy plunge into the center of the earth, but hey, you have to start somewhere. I think you know where this is going.

When character touches the ground, STOP!


Don't forget, IF, THEN.

if the character touches the ground
     then stop him from moving down


This seems like a pretty simple command, but for me, it's another moment where I had to search around the help files, looking for the commands I wanted specifically. The command is "place_meeting(x,y,obj)". Basically, this function checks if there would be collision between the object it is written in, and the object written at "obj". The catch here is that it checks for a hypothetical collision, assuming that the object it's typed into was placed at the x and y you type in. It may be easier to understand if I gave an example.

if place_meeting(25,35,obj_wall)


In other words, if the object was placed at the coordinates 25,35, would it collide with the wall?

For us though, it gets even more complicated.

if place_meeting(x,y+vspeed,obj_floor)
{
     vspeed = 0
}
     else
     {
          vspeed += 2
     }


Want to know why exactly I typed "(x,y+vspeed,obj_floor)"? Well, x and y were entered to keep the collision check relative to my position and added vspeed to y. This literally means it's checking for a collision vspeed pixels below the character's position.

There we go then, our happy little rectangle can now fall daintily onto the floor. Unfortunately, it's trapped on the ground until we teach it to jump. This is the easy part. Think about it for a second...

if player presses up
     then give him upward momentum


Now translate:

if keyboard_check_pressed(vk_up)             // pressed prevents player from holding down the key
{
     vspeed = -27                                          // number is whatever you want, I just liked how 27 felt
}


With that, the character now has the ability to move around by moving, jumping, and falling. We did good, right?

Right?





If you try and type the above commands into Game Maker they will work, but you'll immediately notice several issues. For one thing, your character jitters right before landing on the ground, and for another, you can jump infinitely. Even by B-grade game standards, it's pretty abysmal. We have to fix these issues, but for now, take a moment and try to figure out how to do so yourself. Next time, we fix the choppy platforming, and finally stop the player from walking through the walls.

EDIT:
     Here is the Game Maker file:
          https://www.dropbox.com/s/1xug8y6z276buyv/3%20Rise%20and%20Fall.gm81
     Her is the executable:
          https://www.dropbox.com/s/g0wwrwmfktqnhqz/3%20Rise%20and%20Fall.exe

No comments:

Post a Comment