When it comes to creating games with Flash, one problem seems to come up a lot: jumping.  Giving the player the ability to jump can greatly enhance gameplay, but it does present its own set of challenges.  How do you get the action to feel natural?  How do you prevent the player from jumping again once they’re already in the air?  How do you determine when the character has hit the ground?  All of these are valid questions and I thought I’d share the way I handle jumping in games.  Note that sample files are provided as well.

The first thing we need to do is to declare a few variables that will prove useful in calculating a jump.  When something goes up, it must come down right?  So we’ll create a gravity variable that will push down on our player.  We’ll also need a variable to hold the strength of the character’s jump, a boolean to tell us whether the character is jumping or not and also a variable that tells us the location of the ground.

var gravity:Number = 10;
var jumpPower:Number = 0;
var isJumping:Boolean = false;
var ground:Number = 377 - box_mc.height;

In my example, the jumping object has an instance name of box_mc.  This movieclip’s registration point is the top-left, so I’m accounting for the height when calculating the ground position.  This way the movieclip will stop falling when the bottom of it reaches the ground (as opposed to the top).

Next up, we’ll add a couple of event listeners to the stage in order to handle user input and updating the scene.

stage.addEventListener(MouseEvent.MOUSE_DOWN, doJump);
stage.addEventListener(Event.ENTER_FRAME, update);

When the user presses down on the left mouse button, we want to make the character jump.  The function doJump will take care of setting up the variables to allow this to happen.

function doJump(evt:MouseEvent):void
jumpPower = 30;
isJumping = true;

This method simply checks to make sure the player isn’t already jumping.  If isJumping is false, we set jumpPower to 30 and turn isJumping to true since now our character will be jumping.  Feel free to play around with the jumpPower setting.  You may want to increase or decrease it to fit your own game.

Next let’s define the update method; this method will take care of the actual player movement.

function update(evt:Event):void
box_mc.y -= jumpPower;
jumpPower -= 2;

if(box_mc.y + gravity < ground)
box_mc.y += gravity;
box_mc.y = ground;
isJumping = false;

// for horizontal movement
box_mc.x = stage.mouseX - box_mc.width * 0.5;

What exactly is going on here?  Well, if the player is jumping we move him up by subtracting jumpPower from his current y position.  Then we decrease jumpPower so that it slows down gradually.  This prevents our player from jumping off to infinity and helps bring him back to the ground.  Next we check if adding gravity to the player’s y will push him below the ground.  If not, we’ll go ahead and add it; if so, then we lock the player’s position to ground level and set isJumping to false since the character has returned to the ground.  The last bit just allows some horizontal control over the player.

That’s all there is to it!  Not too bad, right?  I hope this is useful to some of you and if anyone has other ways of handling jumping please feel free to share!