Pages

Wednesday, June 27, 2012

8: Mario Posing

Our character has a face now, but he has an odd tendency to run while jumping in the air. Fixing this next part is pretty easy. How do we stop Tanner from running in the air? How do you determine if he is in the air?

place_meeting(x,y,obj)

Jackpot, sorta. We could just as easily add this to our animation code, but I would rather avoid unnecessary checks when a variable will do just fine. Let's add a new one to the fold:

inair = 0

Then introduce it to our gravity code:


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


There we go. No we have a variable we can call whenever we need to know if Tanner is touching the ground. Now all we need to do is use this in our animation code:

if (inair = 0)
     then run and idle animation
else if (inair = 1)
     then in air animation


If you're following along, you'll soon get to feel the joy of rearranging your code to keep it looking pretty as you add if statements. For now though, we'll just focus on the in air portion. This next portion is a little weird, mainly because I found a goofy way of doing it. First off, we can add:

if (inair = 1)
{
     image_speed = 0
     sprite_index = spr_tinair
}


We know that inair contains 4 frames, but each is intended as a completely different graphic.



Therefore the first thing we do is set the speed to 0. Next, we need to check if Tanner is actually rising or falling. We could possibly use prevy, but we have something else at our disposal that's very helpful, vspeed.

if (inair = 1)
{
     image_speed = 0
     sprite_index = spr_tinair
     if (vspeed < 0)
     {
          image_index = 0
     }
     else if (vspeed > 0)
     {
          image_index = 2
     }
}


We're almost there. Tanner is rising and falling, but he's always facing left. It's time to put face to use, but how? We could add more if statements, but it will look pretty cluttered, and be a bit repetitive (something that's going to be unavoidable later anyway, but stick with me). How about just adding face to image_index?


if (inair = 1)
{
     image_speed = 0
     sprite_index = spr_tinair
     if (vspeed < 0)
     {
          image_index = 0
          image_index += face
     }
     else if (vspeed > 0)
     {
          image_index = 2
          image_index += face
     }
}


Think about it. The variable face is always 0 for left and 1 for right. I arranged the frames in spr_tinair so that the first frame was left, and the second was right. Adding face works perfectly, and now that we know we can do this, we might be able to put it to use at some other point.

With that all done, our animation code (at least for movement) is perfect. Next time, I touch on some small debugging. And  for completion sake, here's the full code:


if (inair = 0)
{
     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
          }
     }
}

else if (inair = 1)
{


     image_speed = 0
     sprite_index = spr_tinair
     if (vspeed < 0)
     {
          image_index = 0
          image_index += face
     }
     else if (vspeed > 0)
     {
          image_index = 2
          image_index += face
     }
}


Here's the gm81 file:
     https://www.dropbox.com/s/70ubcxv1cb76ju6/8%20Mario%20Posing.gm81
Here's the executable:
     https://www.dropbox.com/s/ddy7ztx0fcc4guz/8%20Mario%20Posing.exe



No comments:

Post a Comment