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


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

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 first do the score, since that’s the easiest. Define these variables at the top of your code:

var score:int = 0;//you know what this is
var scoreInc:int = 0;//how many points the bell is going to be worth

Now, find the code in “Bell.as” where the bell is hit by mcMain. Add this code to that:

_root.scoreInc += 10;//increase the amount that the score will increase
_root.score += _root.scoreInc;//add this to the score

Next, find the code where the bird is hit by mcMain. Double the score in that part of the code with _root.score *= 2

Congratulations, we’ve just scored our game! That was the easy part. Now, we have to actually display what each bell is worth after hitting it. In order to do this, we’re going to have to create a new MovieClip. The only thing that’s going to be inside it is a dynamic textfield. Also, when you convert it into a MovieClip, Export it for ActionScript as the class, ScoreAdd, would you now?

Give the dynamic textfield an instance name of txtScore, and set it to 18 point font Arial. Also, you should embed the font, just in case.

Now, create a new layer within that MovieClip and add the following code to it:

var timeLeft:int = 12;//how many frames are allowed to pass before it disappears

this.addEventListener(Event.ENTER_FRAME, timeCheck);

function timeCheck(e:Event):void{
	this.timeLeft --;//decrease the amount of time
	if(this.timeLeft == 0){//if time is up
		this.parent.removeChild(this);//then remove this
	}
}

Now, let’s add this to the stage whenever mcMain hits a bell. Place this code in the hitTestObject for mcMain in “Bell.as”:

var scoreText:ScoreAdd = new ScoreAdd();
_root.bellHolder.addChild(scoreText);//add some text to the stage
scoreText.x = this.x;//set the coordinates for the text
scoreText.y = this.y;
scoreText.txtScore.text = _root.scoreInc;//set the text to the amount the score increased by

Do something similar for the bird:

var scoreText:ScoreAdd = new ScoreAdd();
_root.bellHolder.addChild(scoreText);//add some text to the stage
scoreText.x = this.x;//set the coordinates for the text
scoreText.y = this.y;
scoreText.txtScore.text = 'Double Score';//show that the score was doubled
this.removeEventListener(Event.ENTER_FRAME, eFrame);//remove the listeners
_root.bellHolder.removeChild(this);//and finally remove him from the stage

Now, we must display the score to the user. Just add a dynamic text field to the top of the stage with an instance name of txtScore. Now, in the eFrame() function, just set the text to "Score: "+score.

The final task that we must complete is what happens when the user finally falls. To do this, we’re going to have to create some more variables:

var gameOver:Boolean = false;//if game has been finished
var fallingTime:int = 0;//how long the main guy has been falling down

Before we go any further, add the following code to the eFrame() functions of both the bell and the bird:

if(_root.gameOver){//if the game is over
	this.removeEventListener(Event.ENTER_FRAME, eFrame);//remove the listeners
	_root.bellHolder.removeChild(this);//and remove from stage
}

This will help us later. Next, find this code in 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
}

Change it to this:

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
	
	fallingTime ++;
	
	if(fallingTime >= 12){
		gameOver = true;
	}
}

We have to do this so that if the player gets up really high and falls, there won’t be any more bells to catch him/her and delay the end of the game.

Now, add _root.startedJumping = true; to where the bell and bird hit test for mcMain.

Next, add this code to the part of the mainJump() function where we check if he’s touching the ground. Change it to this:

if(startedJumping){//if the main has begun jumping
	gameOver = true;//then finish the game
	showFinalStats();//and show the poor kid their stats
}
mainJumping = false;

We have to define the showFinalStats() function at the end of the code, but let’s first create a MovieClip that will pop up with the stats. Create a gray 300×250 sized square as a background for this MovieClip. Then, add some text like “Here is your final score:”. Then add a dynamic textfield with an instance name of txtFinalScore below and a “Play Again?” button below that with an instance name of btnPlayAgain. This is what mine looks like:

Now, add the following code to the bottom of everything:

var finalScreen:mcFinalStats = new mcFinalStats();

function showFinalStats():void{
	//attach the final screen to the stage:
	addChild(finalScreen);
	//setting the coordinates
	finalScreen.x = 115;
	finalScreen.y = 75;
	finalScreen.txtFinalScore.text = String(score);//showing the score
	finalScreen.btnPlayAgain.addEventListener(MouseEvent.CLICK, clickPlayAgain);//when user clicks on the button
}

function clickPlayAgain(e:MouseEvent):void{
	//reset everything
	removeChild(finalScreen);
	gameOver = false;
	startedJumping = false;
	score = 0;
	scoreInc = 0;
}

Well, now we can be jubilant, for we are soon finished with this game. Next time, we’ll just fix some issues and add some extras to the game. Join us!

The Final Product

Source Files (zipped)

Read More...

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


Written By MrSun at 8:03 am - Wednesday, January 07th, 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 AS3 – Part 2


Written By MrSun at 8:02 am - Wednesday, January 07th, 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 AS3


Written By MrSun at 8:01 am - Wednesday, January 07th, 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 3.0. Let us begin, shall we? The basic concept of Winter Bells is […]

Read More...

4 Things to Do When Your Mind is Bubbling with Game Ideas


Written By MrSun at 1:26 pm - Monday, January 05th, 2009
Categories: Uncategorized

We’ve all had that feeling (along with it’s counterpart), when our minds are just completely filled with ideas. With all these ideas crowding our head, it is tough to concentrate on any one singular task. I’ll give you a few ways to keep your mind un-jumbled.
Image by Gaetan Lee

Read More...

Link Post Sunday 01/03


Written By MrSun at 12:00 pm - Sunday, January 04th, 2009
Categories: Uncategorized

A collection of the best links I find throughout the weeks, covering topics such as Flash, Flex, Game Development, Humor, and Web Design.

Read More...

Tutorial: Create a Platform Game in AS2 – Part 7


Written By MrSun at 8:06 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 7: Finishing Touches As I’ve done with most of my previous tutorials, this “Finishing Touches” part of the tutorial will mostly be me telling you what to do, but not how […]

Read More...

Tutorial: Create a Platform Game in AS2 – Part 6


Written By MrSun at 8:05 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 6: Goals and Level Completion Ok, now that we’ve added level components and enemies, we can finally make it possible to complete a level. In our game, we’re simply going to […]

Read More...

Tutorial: Create a Platform Game in AS2 – Part 5


Written By MrSun at 8:04 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 5: Adding Enemies The next step in making our exciting platform game is to add some enemies! In order to do this, we’re first going to have to make an Enemy […]

Read More...

Tutorial: Create a Platform Game in AS2 – Part 4


Written By MrSun at 8:03 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 4: Adding Level Elements In this lesson, we’re going to add elements to the level, like obstacles and ladders. Here’s a list of what we’re going to add: Ladders Bumpers Trampolines […]

Read More...