Pages

Tuesday, June 19, 2012

5: Jumping Complex

Before we go any further, I've decided to do something different as I go along. This entire time, I've just been  typing in instructions, and expected people to learn just by reading it, or by following along themselves. That's fairly unreasonable, so I've taken the time to add the .gm81 file and executable to the end of each post where I add code. The executable is there if you don't have Game Maker, and would only like to see the end result (and the glitches). The .gm81 is the source file Game Maker can read, so you can see exactly how I've set things up.

The reason I wasn't doing this before is due to the fact that I'm much farther along than what we've been over. Every example picture I've show, and code is actually me going back through an extra copy of the game and deleting anything we haven't discussed yet. All files are uploaded using DropBox, so you don't have to be attacked by ads and the like. I'd also like to apologize about the executable, as I'm running Game Maker Light. I plan on getting the official version eventually, but until then, these files have the annoying watermark.

Now back to our feature presentation.

The first thing I want to add today is double jumping, but this time, I want the player to be able to do so once. Let's bring up the old code.


if keyboard_check_pressed(vk_up)
{
     if place_meeting(x,y+1,obj_floor)
     {
          vspeed = -27
     }
}


If we look at how our jump code works right now, it only works if the character is on the ground. In order to make sure it works in the air, we need to add an exception.

if keyboard_check_pressed(vk_up)
{
     if place_meeting(x,y+1,obj_floor)
     {
          vspeed = -27
     }
     else if something                                                // exception
     {
          vspeed = -20                                                 // double jump is weaker due to personal preference
     }
}


What exactly is this exception? Well, how about a variable? We can call it doublejump. When the player touches the ground, the variable becomes 1. When the player jumps midair, the variable becomes 0.


if keyboard_check_pressed(vk_up)
{
     if place_meeting(x,y+1,obj_floor)
     {
          vspeed = -27
     }
     else if (doublejump == 1)
     {
          vspeed = -20
          doublejump = 0
     }
}


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


Typing all of this will technically work, but we've got one last thing to do before it works. If you decide to make your own variable (and believe me, you will often), you have to declare it. So in the Create event, add a script that simply says:

doublejump = 1


The number is irrelevent here. Any custom variable must be declared in the create event. If you didn't do that, as soon as the game attempts to do anything with the variable, it declares an error and shuts down.

The next element I want to touch is Dynamic Jumping. If you've played quite a few Platformer games, this is an element you see all the time, but rarely think too much about. We want the player to be able to control their jump height based on how long they hold the jump key. First off, let me introduce a new command.

keyboard_check_released(key)


What we want to do is program so that when the player releases the up key, they stop their upward momentum.

if keyboard_check_released(vk_up)
{
     vspeed = 0
}


That would work, right? Well, not really. If the vspeed becomes 0 instantly every time the player lets go of the up key, the character would stop in midair, even if they were falling. So let's double up the conditions just like we did with the basic jumping code.

if keyboard_check_released(vk_up)
{
     if (vspeed < 0)
     {
          vspeed = 0
     }
}


The "vspeed < 0" condition checks if the character has upward moment before stopping the vertical movement. This does need to be adjusted a bit though, as the character's jump will appear a tad jerky. I want the character to still rise slightly after the key is released, so I want the vspeed to be set to -10.


if keyboard_check_released(vk_up)
{
     if (vspeed < 0)
     {
          vspeed = -10
     }
}


This brings up one final, easy to fix problem. If the player releases the key when their vspeed is between -10 and 0, they will actually gain jump height. In order to remove this, we just change:


if keyboard_check_released(vk_up)
{
     if (vspeed < -10)
     {
          vspeed = -10
     }
}


And there we go. With that out of the way, we are completely done with movement, at least for now. Next, we add some actual graphics, and turn our little rectangle into a real boy.

Here is the Game Maker file:
     https://www.dropbox.com/s/7ylg2uhvucq4hrv/5%20Jumping%20Complex.gm81
Here is the executable file:
     https://www.dropbox.com/s/ovezllkl6cnkqrj/5%20Jumping%20Complex.exe

No comments:

Post a Comment