Tutorial: Create a Platform Game in AS3 – Part 3
Categories: Flash
Table of Contents
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
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
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 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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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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
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
its better to change the jumpSpeedLimit to 25 if you want a nicer jump, with 20 youll see a delayed move in the jump
October 31st, 2008 at 4:16 pm
After I was done at this stage, my guy just falls down to oblivion with no blocks around..
November 26th, 2008 at 4:01 pm
I get an error for all of the mainOnGround parts, access of undefined property. Help?
Thanks, good tutorial so far. =)
March 5th, 2009 at 6:42 pm
How would I do this with things that I drew because I don’t want it to be all blocky
March 29th, 2009 at 3:30 pm
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.
April 1st, 2009 at 1:01 am
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…
April 4th, 2009 at 3:18 am
newBlock:Block = new Block(); . It doesn’t work, I receieve errors about it. Could anyone help? 🙂
April 7th, 2009 at 11:08 am
Awsoome. But i dont get something, what excactly does “for” and “break” do?
May 24th, 2009 at 8:47 pm
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?
June 28th, 2009 at 8:27 am
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
September 11th, 2009 at 10:53 am
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…
November 20th, 2009 at 5:37 pm
on the first part im not sure exactly what code to replace because what ive tried so far just brings up many errors
April 13th, 2010 at 7:37 am
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 😛
May 4th, 2010 at 2:48 am
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
May 6th, 2010 at 10:46 am
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.
May 13th, 2010 at 12:43 pm
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.
June 8th, 2010 at 12:53 am