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

Written By MrSun at 8:03 am - Wednesday, January 07th, 2009
Categories: Flash

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 can actually move up the levels. Define this function at the end of your code:

function moveScreen():void{ //function that moves the screen up and down
	if(mcMain.y <= 125){//if main gets up high enough
		mcMain.y = 125;//don't let him jump any higher
		bellHolder.y -= jumpSpeed;//instead, move the entire level down
		bellTop += jumpSpeed;//we also have to move the creation point for new bells
		bellLimit = 6;//make the bells appear quicker since we're moving up quicker
	} else {
		bellLimit = 20;//change it back if we're done

Now, run it in the eFrame() function. Right now, all this code will do is make the level go upwards for the main character, but not back down. In order to accomplish this feat, we have to define some variables at the top:

var totalHeight:Number = 0; //how far up the main guy has travelled
var startedJumping:Boolean = false;//whether or not main has actually hit a bell yet

Now, we're going to have to add and subtract to the totalHeight whenever the character jumps and falls. This is actually easier than it seems. All we have to do is add totalHeight -= jumpSpeed; to the mainJump() function. Not too bad, eh? Now, add this code to the moveScreen() function:

if(mcMain.y >= 275 && totalHeight >= 275){ //if mcMain is above a certain point and the screen has been moved up
	bellHolder.y -= jumpSpeed;//make bellHolder go back down
	bellTop += jumpSpeed;//as well as the creation point
	mcMain.y = 275;//keep mcMain's y stationary until it is done falling

Now, the game should work out pretty fine. We're actually pretty much done with this portion of the tutorial. Please, do join us next time when we add some scoring factors to this lil' game of ours.

The Final Product

Source Files (zipped)

One Comment


I think there’s something missing in this section, since I can’t get the main character to keep falling the collected distance (it does scroll up though, but not down.)

Not sure how to amend it completely on my own, you have “startedJumping” declared but not used anywhere, I’m guessing I can add “totalHeight -= jumpSpeed;” anywhere within the function (it doesn’t have to be in any of the “if” statements?), and I tried changing it to “totalHeight += jumpSpeed;” but all it does is cause the ball to fall forever once it jumps from the start without hitting any bells.

Gonna try the source code itself out, but I don’t think some things are porperly explained in this tutorial. That or I really messed something up.