How to Create a Platform Game in AS2 – Part 1

Part 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.
My character guy
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!

Spread the Knowledge

  • Print
  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • Twitter
  • Google Bookmarks
  • Design Float
  • DZone
  • Yahoo! Buzz
  • PDF
  • Reddit
  • RSS
  • Technorati

Preview

Download Source
(Requires Flash 8 or above)

Subscribe!

Subscribe!
Enter your email address:  

Awesome Tutorials