Tutorial: Create a Platform Game in AS2 – Part 3


Written By MrSun at 8:02 am - Saturday, January 03rd, 2009
Categories: Flash

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
//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;
}

with this:

//if main hits a block, then stop jumping
//this loop will check a hit test with any block
for(var cBlock:String in blockHolder){
	//cBlock is simply the name of the chosen block
	//we can use this to access and do what we want with it
	if(mcMain.hitTest(blockHolder[cBlock]) && mcMain._y <= hitBlock._y){
		mainOnGround = true;
		//stop jumping
		mainJumping = false;
		//and set the guys coordinates to be on top of the block
		mcMain._y = blockHolder[cBlock]._y - mcMain._height;
		//break out of the loop to save processor power
		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 the main onEnterFrame() function:

//checking if he isn't on the ground
//run a loop checking if he's touching blocks
for(var cBlock:String in blockHolder){
	//checking hit test and if main is above the brick
	if(mcMain.hitTest(blockHolder[cBlock]) && mcMain._y < blockHolder[cBlock]._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 the 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 cBlock:String in blockHolder){
	//cBlock is simply the name of the chosen block
	//we can use this to access and do what we want with it
	if(mcMain.hitTest(blockHolder[cBlock])){
		//if main is falling down
		if(jumpSpeed > 0){
			//stop jumping
			mainJumping = false
			//and set the guy's coordinates to be on the top of the block
			mcMain._y = blockHolder[cBlock]._y - 25;
			//he's now on solid ground
			mainOnGround = true;
			//break out of the loop to save processing power
			break;
		} else {
			jumpSpeed = Math.abs(jumpSpeed);
			//making the main guy get away from the block
			mcMain._y = blockHolder[cBlock]._y + blockHolder[cBlock]._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 go to work. Replace the current function with this code:

function createLvl():Void{
	//finding the array of the current level we're on
	//this is just a way to dynamically select an Array within the document
	var lvlArray:Array = _root['lvlArray'+lvlCurrent];
	//we have to find how far this level will span
	//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 level formatting very strict
	var lvlColumns:Number = Math.ceil(lvlArray.length/16);
	//now we can create the level
	for(var i:Number=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 main onEnterFrame() function, replace:

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
}

with:

if(Key.isDown(37) || Key.isDown(65)){ //if the "A" key or Left Arrow Key is Down
	blockHolder._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
	blockHolder._x -= mainSpeed; //then move the guy to the right
}

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:

//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;

Replace it with this:

//if we're already jumping, then continue to do so
if(jumpSpeed < 0){
	jumpSpeed *= 1 - jumpSpeedLimit/125;
	if(jumpSpeed > -jumpSpeedLimit*.2){
		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 .fla file

Read More...

Tutorial: Create a Platform Game in AS2 – Part 2


Written By MrSun at 8:01 am - Saturday, January 03rd, 2009
Categories: Flash

Table of Contents Basic Character Programming Creating The Level Programming the Level Adding Level Elements Adding Enemies Goals and Level Completion Finishing Touches Step 2: Creating the Level Now, we have to set up blocks on stage that will account for a level. We’ll use an array to accomplish this manly feat. We’re also going […]

Read More...

Tutorial: Create a Platform Game in AS2


Written By MrSun at 8:00 am - Saturday, January 03rd, 2009
Categories: Flash

Table of Contents Basic Character Programming Creating The Level Programming the Level Adding Level Elements Adding Enemies Goals and Level Completion Finishing Touches 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 […]

Read More...

Tutorial: Create a Game Like Winter Bells in AS2 – Part 5


Written By MrSun at 8:05 am - Thursday, January 01st, 2009
Categories: Flash

Basic Character Programming Programming the “Bells” Level Creation Scoring Finishing Touches Step 5: Finishing Touches This part of tutorial is where we add some bug fixes and special effects. Let’s get started. The first bug fix that we have to make is one related to jumping. I probably should have noticed this beforehand but with […]

Read More...

Tutorial: Create a Game Like Winter Bells in AS2 – Part 4


Written By MrSun at 8:04 am - Thursday, January 01st, 2009
Categories: Flash

Basic Character Programming Programming the “Bells” Level Creation Scoring Finishing Touches Step 4: Scoring Now, we get to score this game. In addition to adding a score, we’re also going to add a few other components, including displaying the points each bell is worth, as well as the game over stuff. Shall we begin? Let’s […]

Read More...

Tutorial: Create a Game Like Winter Bells in AS2 – Part 3


Written By MrSun at 8:03 am - Thursday, January 01st, 2009
Categories: Flash

Basic Character Programming Programming the “Bells” Level Creation Scoring Finishing Touches Step 3: Level Creation Welcome to the third part of this tutorial. In this part, we’re going to create the entire thing into a functional level. The first thing we’re going to do is make the screen move along with the character, so it […]

Read More...

Tutorial: Create a Game Like Winter Bells in AS2 – Part 2


Written By MrSun at 8:02 am - Thursday, January 01st, 2009
Categories: Flash

Basic Character Programming Programming the “Bells” Level Creation Scoring Finishing Touches Step 2: Programming the “Bells” Let’s get right to it. In this part of the tutorial, we’re going to add the “bells” to the stage. They’ll be 100% randomized so we can continue playing the game forever (You’d like that, wouldn’t you?). Anyway, we […]

Read More...

Tutorial: Create a Game Like Winter Bells in AS2


Written By MrSun at 8:01 am - Thursday, January 01st, 2009
Categories: Flash

Basic Character Programming Programming the “Bells” Level Creation Scoring Finishing Touches Step 1: Basic Character Programming Welcome, people, and get ready for yet another tutorial! In celebration of the winter season, we’re going to make a game like Winter Bells, in ActionScript 2.0. Let us begin, shall we? The basic concept of Winter Bells is […]

Read More...

11 Tutorial Sites for Learning Flash


Written By MrSun at 12:56 pm - Sunday, December 28th, 2008
Categories: Flash

Flash Kit Actionscript.org Kirupa GotoAndLearn Pixel2Life Tutorialized Good Tutorials Tutorials Garden Tut City Flash Magazine Flash Perfection

Read More...

Merry Flippin’ Christmas!


Written By MrSun at 11:13 am - Thursday, December 25th, 2008
Categories: Uncategorized

Merry Christmas, everyone! As a gift for everyone, I’ve put up the new re-design! Hopefully, you’ll like it!

Read More...