Tutorial: Create a Game Like Winter Bells in AS3 – Part 4
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!
awesome tutorial. I really enjoyed building it. Thanks for taking the time to write this.
January 12th, 2009 at 7:26 pm
its been great so far, but i cant get it to work past (We have to define the showFinalStats) part.
August 22nd, 2009 at 7:32 am
I want to ask you something. How can I do. I want to do that. When the user click play again button, play game from the first frame in the main root. thnk you for help
March 9th, 2010 at 3:27 pm