ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Jumping in Video Games
http://www.actionscript.org/resources/articles/1086/1/Jumping-in-Video-Games/Page1.html
Ryan Schaefer
I have been programming with Flash and ActionScript professionally for over 5 years. I have done work for several high-profile clients including Electronic Arts, MasterCard and Lightmaker and have taught ActionScript training courses for Aquent Software. I believe my experience has provided me with some knowledge that other developers may find useful so here I am! :)
Published on May 2, 2011

This short tutorial covers the basic concepts behind making a character jump in a video game.

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.

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

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.

[as]
[/as]

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.

[as]
function doJump(evt:MouseEvent):void
{
if(!isJumping)
{
jumpPower = 30;
isJumping = true;
}
}
[/as]

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.

[as]
function update(evt:Event):void
{
if(isJumping)
{
box_mc.y -= jumpPower;
jumpPower -= 2;
}

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

// for horizontal movement
box_mc.x = stage.mouseX - box_mc.width * 0.5;
}
[/as]

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!