Tutorial: Create a Platform Game in AS3 – Part 3


Written By MrSun at 8:02 am - Saturday, August 30th, 2008
Categories: Advanced Tutorials, All Tutorials, AS3, Flash, Game Development

Step 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…

Final Product

Source Files (zipped)

16 Comments

fercho:

its better to change the jumpSpeedLimit to 25 if you want a nicer jump, with 20 youll see a delayed move in the jump


Greg:

After I was done at this stage, my guy just falls down to oblivion with no blocks around..


Blarg:

I get an error for all of the mainOnGround parts, access of undefined property. Help?
Thanks, good tutorial so far. =)


bleg:

How would I do this with things that I drew because I don’t want it to be all blocky


eregon:

Blarg:

you have to make a var for mainOnGround and make it a boolean false

and i have the same problem as Greg, it happened when i changed the createlevel code.


Jon:

So I have the same problem as Greg…I fixed it (kindof) by starting my level array with a column of 1s, don’t know why that fixed it? …and I’m translating this for Flex 3 if anyone has any simple questions about that I might can help…


Mig själv:

newBlock:Block = new Block(); . It doesn’t work, I receieve errors about it. Could anyone help? :)


Greg:

Awsoome. But i dont get something, what excactly does “for” and “break” do?


James:

It workS great… exept I keep getting namespace errors here:
for(var i:int = 0;i<blockHolder.numChildren; i++){
It says that I has a conflict. Can you help?


ryan Felton:

for means a start of a loop (think of it as ‘while’ but keep syntax to for.

example: for(i:int = 0; i<6; i++)
meaning: i = 0, the loop will keep adding 1 untill i = 6.

break means the end of a loop


MartinThatcher:

For some reason, when I edited the mainJump() function, my character was unable to jump… I’m still having the problem even after copying the source code…


lolfish:

on the first part im not sure exactly what code to replace because what ive tried so far just brings up many errors


jag:

for blarg and Greg, I had the same problem. what it was, was that I had deleted createLvl(); so when the code didn’t get a call from somewhere, it didn’t make the lvl :P


Dinges:

Everything works fine except my guy just wont jump…
BTW: my main movieclip is 25×25 and i copied the script from your source file because I had some other errors


Zilx:

ok… i will trouble shoot a little…

Bleg, make something, convert to a symbol, under advanced, check the export for action script. this will mean that you will ahve to have a seperate AS file for anything you want to happen, so right now the only thing you can change is the blocks, if you take all the player code and put it into an AS then you can make a player too.

Greg, For puts the program into a loop, break states the end of the loop.

Ok, my turn for a problem now.
When i download the Zip file and try to run it, all that happens is that the little circle shows up matched with the floor, the arrow keys dont work, no blocks show up.


Ahmad:

Amazing tutorial :D , i was able to do it on my second day of programming action script. (action script is the first programming language i have learned/am learning about.


«
»