Tutorial: Create a Platform Game in AS2
Categories: AS2, Advanced Tutorials, All Tutorials, Flash, Game Development
Table of Contents
Due to popular request, I’ve decided to make an AS2 version of my tutorial. For consistency, it will follow the same format as the AS3 version and will also use excerpts of the same text. Here we go.
Step 1: Basic Character Programming
Let’s first begin with the basic setup of our game. I’m going to make the background black and the frame rate 24 fps (like in previous tutorials). The first thing to make is our little character. I won’t be too artistic with this one, just a little white circle without any outline.

Its dimensions are 25×25 pixels.
Next, convert it into a MovieClip and give it an instance name of mcMain. Now, we can add code to move this character.
Just create a new “actions” layer and place this code in the first frame (DON’T ADD IT TO THE MOVIECLIP):
var mainSpeed:Number = 7; //The speed of the character onEnterFrame = function(){ //this function will run every frame (needed for moving the character if(Key.isDown(37) || Key.isDown(65)){ //if the "A" key or Left Arrow Key is Down mcMain._x -= mainSpeed;//then the move the guy left } else if(Key.isDown(39) || Key.isDown(68)){//if the "D" key or Right Arrow Key is Down mcMain._x += mainSpeed; //then move the guy to the right } }
This code will simply make the character move left and right. Now, we have to make the character jump. We’ll accomplish this with a jump function and running it whenever the up key is down. First, we have to define a few variables at the top of the code:
//JUMPING VARIABLES //whether or not the main guy is jumping var mainJumping:Boolean = false; //how quickly should the jump start off var jumpSpeedLimit:Number = 15; //the current speed of the jump; var jumpSpeed:Number = jumpSpeedLimit;
Then, we’ll define a function which will make the guy jump. It’ll take some moderately complicated math. Add this code to the bottom of your code:
//THE JUMPING FUNCTION function mainJump():Void{ //if main isn't already jumping if(!mainJumping){ if(Key.isDown(38) || Key.isDown(87)){ //then start jumping mainJumping = true; jumpSpeed = jumpSpeedLimit*-1; mcMain._y += jumpSpeed; } } else { //if we're already jumping, then continue to do so if(jumpSpeed < 0){ jumpSpeed *= 1 - jumpSpeedLimit/75; if(jumpSpeed > -jumpSpeedLimit*.2){ jumpSpeed *= -1; } } if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){ jumpSpeed *= 1 + jumpSpeedLimit/50; } mcMain._y += jumpSpeed; //if main hits the floor, then stop jumping //we'll have to change this however when we create the blocks in the level if(mcMain._y >= Stage.height - mcMain._height){ mainJumping = false; mcMain._y = Stage.height - mcMain._height; } } }
Pretty intense, eh? The last thing we need to do in order to make the jumping work is to add the mainJump() function to the enterFrame. This is would we should change it to:
onEnterFrame = function(){ //this function will run every frame (needed for moving the character if(Key.isDown(37) || Key.isDown(65)){ //if the "A" key or Left Arrow Key is Down mcMain._x -= mainSpeed;//then the move the guy left } else if(Key.isDown(39) || Key.isDown(68)){//if the "D" key or Right Arrow Key is Down mcMain._x += mainSpeed; //then move the guy to the right } mainJump(); }
Congratulations! We’ve just made the character move and jump! Next time, we’ll create a level with blocks we can walk on!





This tutorial is quite helpful but lets say i would like to make a character accually walk, and jump as opposed to simply move. I would assume i would involve making a movie clip for walking and jumping followed by using action script to swap them in for the standing frame once a button is pressed.
The thing is i have no idea how to do what i just said above or if it was a correct assumption in the first place.
Also i was wondering if the same concept would apply to enimmies and world objects such as the bumpers.
March 27th, 2009 at 5:10 pm
newbie here but i copied the script in per word except but it gives me on/onClipEvent error on a few lines of code. how do i Implement this correctly?
April 22nd, 2009 at 2:40 am
Not nay saying or anything, but surely for the jump scripting, couldn’t you just use this?
var yspeed = 1;
var gravity = 1;
var jump = -8;
onEnterFrame = function () {
mcMain._y += yspeed;
yspeed += gravity;
if(mcMain.hitTest(ground)){
if(Key.isDown(Key.UP)){
yspeed = jump;
} else {
yspeed = 0;
}
}
}
May 23rd, 2009 at 10:59 am
hi, i have try making this game and its work. But then, i have a problem to change the keys. Can u give me the coding for change the directional keys to a,s,d,f keys…im still new new in as2~
May 25th, 2009 at 12:21 pm
how do i change the hight of the jump?
June 7th, 2009 at 10:23 pm
i have written this for an hour and then nothing happened doesn’t work for me i guess
July 2nd, 2009 at 2:47 am
the jump script doesnt work, my char moves but doesnt jump
July 21st, 2009 at 4:37 pm
the same thing happend to me at first but i knew how to fix it up.
you have to copy the last code instead of the on before it; so the final shape of the code should look like this:
var mainSpeed:Number = 7;
onEnterFrame = function(){ //this function will run every frame (needed for moving the character
if(Key.isDown(37) || Key.isDown(65)){ //if the “A” key or Left Arrow Key is Down
mcMain._x -= mainSpeed;//then the move the guy left
} else if(Key.isDown(39) || Key.isDown(68)){//if the “D” key or Right Arrow Key is Down
mcMain._x += mainSpeed; //then move the guy to the right
}
mainJump();
}
//JUMPING VARIABLES
//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:Number = 15;
//the current speed of the jump;
var jumpSpeed:Number = jumpSpeedLimit;
//THE JUMPING FUNCTION
function mainJump():Void{
//if main isn’t already jumping
if(!mainJumping){
if(Key.isDown(38) || Key.isDown(87)){
//then start jumping
mainJumping = true;
jumpSpeed = jumpSpeedLimit*-1;
mcMain._y += jumpSpeed;
}
} else {
//if we’re already jumping, then continue to do so
if(jumpSpeed -jumpSpeedLimit*.2){
jumpSpeed *= -1;
}
}
if(jumpSpeed > 0 && jumpSpeed = Stage.height – mcMain._height){
mainJumping = false;
mcMain._y = Stage.height – mcMain._height;
}
}
}
January 9th, 2010 at 12:30 am
and for you eric you can always use “for examble” :
if (Key.isDown(Key.LEFT)){ ….
or you can visit this website to view Key code values :
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001136.html
((NOT ADVERTISING))
i am a newbie my self but i always use google and Flash Help topics to find answers !!
hehe
January 9th, 2010 at 1:29 am