Pages

Thursday, June 14, 2012

2: Goodbye Drag n Drop

Now that we've got a basic set up, let's get to the real stuff. Today, we're going to make our special little rectangle move to the left and the right. Nothing difficult, but it's still a challenge for first timers. First things first, we open up obj_tanner's interface and drag an "Execute Code" action into a "Step" event. I can easily recall the next moment in my mind.


Every time I see this, my mind goes just as blank.
I'm not an idiot. I know what kind of commands I need to type. Unfortunately, I have no idea what exactly those commands are, or what way I need to type them. It's a little sad to admit, but I spent about five hours reading guides just to get a general idea of how to program, and find all the command lists. Before I spit out the commands, let's think what we want to accomplish.

Let the player move their character right and left


Very simple command, but the computer doesn't speak English. Computers talk in IF, THENS, so let's translate a bit.

If the player presses right or left
     Then move their character right or left


Getting closer, but if you translated that to GML literally, the character is going to move randomly left or right if the player presses either button.

If the player presses right
     Then move their character right
If the player presses left
     Then move their character left


There we go. Programming is an endless stream of IF statements, so you'd better get used to them. Next, let's actually use some GML commands.

If keyboard_check(vk_right)
{
     Then move their character right
}
If keyboard_check(vk_left)
{
     Then move their character left
}


Okay, the way I typed it isn't how it has to be written, but I like typing it C style, since it keeps things nice and orderly. As you can probably guess though "move their character" isn't a legitimate GML command. There are built in "movement" functions within GML, but sometimes the easiest solution is the best.


If keyboard_check(vk_right)
{
     x += 8
}
If keyboard_check(vk_left)
{
     x -= 8
}


If the newest addition doesn't make any sense, the command "+=" adds the following number to the preceding variable. In other words, add 8 to x. So in layman's terms, as long as the player is pressing right, it constantly adds 8 to the character's x placement, warping it to the right, but at 30 frames per second it looks like it's moving fairly smoothly.

There you have it. The most basic of commands, and something anybody who has made games before can do without a second thought. I still wanted to linger on it, just for the sake of showing some basic logic used in programming.





Whether you're following along in Game Maker, are pretty old hat with programming, or are just that perceptive, you might have noticed a big problem with the above code. If you didn't notice it, look at it again, and think (and no, it has nothing to do with the walls).

Think you've got it? Try tapping both the left and right arrow keys at the same time.

If you press both keys at the same time, both IF statements are performed. It's not a terrible problem, especially considering we're only adding and subtracting from the character's x, meaning pressing both keys just freezes him in place, but if more complex movement elements such as acceleration were used, it could really cause some bugs. There's an extremely easy fix for this though, and it is only one word.



If keyboard_check(vk_right)
{
     x += 8
}
else
{
     If keyboard_check(vk_left)
     {
          x -= 8
     }
}


With the else command entered, the computer always ignore the left key if the right key is already held down. Problem solved. Join me next time when we add gravity to the mix, and teach our little rectangle to jump.

EDIT:
     Here's the Game Maker file:
          https://www.dropbox.com/s/ak8gt8pi63wml2x/2%20Goodbye%20Drag%20n%20Drop.gm81

     Here's the executable:
          https://www.dropbox.com/s/48aoltwaf7s1lys/2%20Goodbye%20Drag%20n%20Drop.exe

1 comment:

  1. Nice Ace Attorney reference. It shows what you've been doing recently.

    ReplyDelete