How to Create a Platform Game in AS3 – Part 3

Part 3: Programming the Level

Now that we’ve set up all of the blocks onto the stage, we can actually add some code to them. The first thing we’re going to do is to make it so the blocks are the only thing that the main guy can stand on. Replace this code in the mainJump() function:

//if main hits the floor, then stop jumping
//of course, we'll change this once we create the level
if(mcMain.y >= stage.stageHeight - mcMain.height){
	mainJumping = false;
	mcMain.y = stage.stageHeight - mcMain.height;
}

With this:

//if main hits a block, then stop jumping
//this loop will check a hit test with any block
for(var i:int = 0;i<blockHolder.numChildren; i++){
	//getting the current block
	var hitBlock:DisplayObject = blockHolder.getChildAt(i);
	//checking hit test and if main is above the brick
	if(mcMain.hitTestObject(hitBlock) && mcMain.y < hitBlock.y){
		mainOnGround = false;
		//stop jumping
		mainJumping = false;
		//and set the guys coordinates to be on top of the block
		mcMain.y = hitBlock.y - mcMain.height;
		//break out of the loop
		break;
	}
}

Now, we have to make the character fall when he isn’t on a block. First, define a variable, mainOnGround at the top, and set it as a false boolean. Then, set it to true if mcMain hits a block in the mainJump() function. Hopefully, I don’t have to walk you through this one. Next, add the following code to moveChar():

//checking if he isn't on ground
//run the loop checking if he's touching any blocks again
for(var i:int = 0;i<blockHolder.numChildren; i++){
	//getting the current block
	var hitBlock:DisplayObject = blockHolder.getChildAt(i);
	//checking hit test and if main is above the brick
	if(mcMain.hitTestObject(hitBlock) && mcMain.y < hitBlock.y){
		//this time, we just make mainOnGround true
		mainOnGround = true;
		//and break from the loop
		break;
	}
 
	mainOnGround = false;
}
 
//then we make him fall if he isn't on ground
if(!mainOnGround){
	mainJumping = true;
}

The next thing we’re going to do is make the guy bounce off of the bricks if he hits them below. Otherwise, he’d somehow get on top of the bricks from below, which we don’t want. To do this, we’re going to have to change the for loop in the mainJump() function. Here’s what you should replace it with:

for(var i:int = 0;i<blockHolder.numChildren; i++){
	//getting the current block
	var hitBlock:DisplayObject = blockHolder.getChildAt(i);
	//checking hit test
	if(mcMain.hitTestObject(hitBlock)){
		//if main is falling down
		if(jumpSpeed > 0){
			//stop jumping
			mainJumping = false;
			//and set the guys coordinates to be on top of the block
			mcMain.y = hitBlock.y - mcMain.height;
			//he's on solid ground
			mainOnGround = true;
			//break out of the loop
			break;
		} else {
			jumpSpeed = Math.abs(jumpSpeed);
			//making the main guy get away from the block
			mcMain.y = hitBlock.y + hitBlock.height + 1;
		}
 
	}
}

Now, if you test it out, the guy bounces when hitting the block. Now, the last thing we’re going to do is make the background move with the character if he moves too far to the left or right. In order to do this, I’m going to change around lvlArray1 so the changes are more necessary. Here’s what I’ll change it to:

var lvlArray1:Array = new Array(
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,X,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
);

Now, we have to edit the createLvl function first it centers the main character and actually makes this code work. Replace the current function with this code:

function createLvl():void{
	//getting the current level that we are on
	var lvlArray:Array = MovieClip(root)['lvlArray'+lvlCurrent];
	//we have to find how far this level goes
	//this will be used so we know when to move to the next row
	//there will always be 16 rows, so this is how we find it out
	//of course, this will make the lvl formatting very strict
	var lvlColumns:int = Math.ceil(lvlArray.length/16);
	//now we must create the level
	for(var i:int = 0;i<lvlArray.length;i++){
		//checking if we move onto the next row
		//this checks if i is divisible by the # of columns
		if(i/lvlColumns == int(i/lvlColumns)){
			row ++;
		}
		if(lvlArray[i] == 1){
			//making a new block
			var newBlock:Block = new Block();
			//drawing the block
			newBlock.graphics.beginFill(0xFFFFFF/*The color for shape*/,1/*The alpha for the shape*/);
			//turning the shape into a square
			newBlock.graphics.drawRect(0,0,25,25);
			//change the coordinates of the block
			newBlock.x = (i-(row-1)*lvlColumns)*newBlock.width;
			newBlock.y = (row-1)*newBlock.height;
			//then finally adding it to stage
			blockHolder.addChild(newBlock);
		} else if (lvlArray[i] == 'MAIN'){
			mcMain.x = (i-(row-1)*lvlColumns)*newBlock.width;
			mcMain.y = (row-1)*newBlock.height;
		}
	}
	//reset the row for another use
	row = 0;
	//then we center the screen on the main character
	//this variable will tell us how far away we have to move everything
	//250 is about centered for mcMain we'll use that number
	var mainMove:int = 250 - mcMain.x;
	//then move mcMain and the entire level to center on him
	mcMain.x += mainMove;
	blockHolder.x += mainMove;
}

This code makes me very happy. The screen will now center on the main character in every level, no matter where you place him. Next, we want the actual background to move instead of the character when he moves so we can keep track of him at all times. This’ll be easy. In the moveChar() function, replace:

if(leftKeyDown){
	mcMain.x -= mainSpeed;
}
if(rightKeyDown){
	mcMain.x += mainSpeed;
}

with this:

if(leftKeyDown){
	blockHolder.x += mainSpeed;
}
if(rightKeyDown){
	blockHolder.x -= mainSpeed;
}

Pretty easy, eh? Now there’s only one problem I want to fix before I end this lesson. The character doesn’t really jump high enough. So, we’re just going to recode this part a bit:

//then continue jumping if already in the air
//crazy math that I won't explain
if(jumpSpeed < 0){
	jumpSpeed *= 1 - jumpSpeedLimit/75;
	if(jumpSpeed > -jumpSpeedLimit/5){
		jumpSpeed *= -1;
	}
}
if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
	jumpSpeed *= 1 + jumpSpeedLimit/50;
}
mcMain.y += jumpSpeed;

Replace it with this code:

//then continue jumping if already in the air
//crazy math that I won't explain
if(jumpSpeed < 0){
	jumpSpeed *= 1 - jumpSpeedLimit/125;
	if(jumpSpeed > -jumpSpeedLimit/5){
		jumpSpeed *= -1;
	}
}
if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
	jumpSpeed *= 1 + jumpSpeedLimit/50;
}
mcMain.y += jumpSpeed;

Also, change the jumpSpeedLimit to 20, eh?

Now, we’re done with the intense block programming. Next lesson, we’ll add some stuff to the level, like ladders and other obstacles. We also have to make it so that when we run into a wall, we’re stopped by it. Stay tuned…

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 CS3 or above)

Subscribe!

Subscribe!
Enter your email address:  

Awesome Tutorials