Pages

Monday, June 25, 2012

7: Run Tanner Run

Welcome back. Last time, we came up with some fancy new variables, and now, we've got to put them to use.

prevx
prevy
playerdir
prevplayerdir
face


I've already explained what these variables are for, but they aren't built into Game Maker. If we want them to do what we intend, we'll have to do it manually. The "prev" line of variables are just used to compare old variables to new ones. Therefore, to make them work, we just have to type:

prevx = x
prevy = y
prevplayerdir = playerdir


The most important part here is placement. We want to put the above code at the very beginning of our "Step" Event, before all our keyboard checks and the like. This way, the "prev" line of variables will be identical to the x, y, and playerdir of the previous frame. Then the rest of the code can mess with the current x and y, and we can compare them afterwards.

The next bit of code is actually going to be put into a new Event, called "End Step". Using "End Step" just guarantees that all movement and collision processing is done already, and all our animation code is applied to the correct situation. This next part is where I got a bit overwhelmed myself, but it's always important to take things one at a time. For now, let's start by checking the player's direction. Remember this statement?


if player is moving leftward
     set character sprite to run left
if player is moving rightward
     set character sprite to run right
if player is not moving
     set character sprite to idle


Ignoring the part about setting the character sprite, let's use this as a guideline:

if (prevx > x)
{
     playerdir = 0
     face = 0
}
else if (prevx < x)
{
     playerdir = 1
     face = 1
}
else if (prevx == x)
{
      playerdir = 2
}


With this done, we now have a piece of code that let's us ask about the player's direction and orientation, and then store that information into our two variables for later use. Unlike adding similar code to the keyboard checking, we're actually basing our information on what is happening instead of what we assume will happen. With this in our hands, the next part, actually changing the character's sprite, is fairly easy. Of course, I had to dig up some new variables built into Game Maker, and here they are:

sprite_index          //A non-numeric string variable that defines which sprite the object is using
image_speed         //A variable that determines the speed at which the sprite switches frames
image_index         //A variable that says which frame the sprite is on


These are fairly self explanatory, although I often find myself mixing them up. The only particularly odd thing to note is image_speed. The number in image_speed is the number of frames of animation per step (and default number of steps per second is 30 in Game Maker). In other words, you generally want to enter a fraction, or your animation will either skip frames or just play extremely fast. Anyway, let's get to coding:


if (playerdir = 0)
{
     sprite_index = spr_trunl
     image_speed = (1/3)
}
else if (playerdir = 1)
{
     sprite_index = spr_trunr
     image_speed = (1/3)
}
else if (playerdir = 2)
{
     image_speed = 0
     if (face = 0)
     {
          sprite_index = spr_tidlel
     }
     else if (face = 1)
     {
          sprite_index = spr_tidler
     }
}


The first two if's seem pretty straightforward. If the player is moving left, give him the left graphic and set his speed to one frame every three steps. If the player is moving right, do the same, but with the right graphic. The second is a little wierder, but not too complex. If the player is standing still, we set the image_speed to 0 (although a bit arbitrary, considering the idle graphics are one frame each. I think originally they were together, but I left it in anyway). Then we check his orientation, and set the graphic to the left or right idle graphic accordingly.

There is one other bit of code I want to add, if only for my own sake. It isn't anything too important, but some people may get annoyed if I didn't add this:

if (prevplayerdir != playerdir)
{
     image_index = 0
}


What does this do? Well, "!=" basically is the same as the NOT operator. In other words, if the player's previous direction is different from his current direction, then the animation gets set to the beginning. This is something that's barely noticeable, but without it, when the player switches from idle to run, or from left run to right run, they will continue mid animation. Again, not something too big, but I wanted to add it anyway.

Now that our hero can run, we have to teach him to stop running midair, and learn proper aerial form. Next time, we add rising and falling graphics to the mix.

Here is the gml81 file:
     https://www.dropbox.com/s/3sye6imavpflnet/7%20Run%20Tanner%20Run.gm81
Here is the executable:
     https://www.dropbox.com/s/5mxbbbzszjecynh/7%20Run%20Tanner%20Run.exe

No comments:

Post a Comment