How to Create a Game Like Winter Bells in AS2 – Part 4
Part 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:Number = 0;//you know what this is
var scoreInc:Number = 0;//how many points the bell is going to be worth
Now, find the code 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 += 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, and some code. Also, when you convert it into a MovieClip, Export it for ActionScript as mcScoreAdd, 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:Number = 12;//how many frames are allowed to pass before it disappears
this.onEnterFrame = function(){
this.timeLeft --;//decrease the amount of time
if(this.timeLeft == 0){//if time is up
this.removeMovieClip();//then remove this
}
}
Now, let’s add this to the stage whenever mcMain hits a bell. Place this code in the hitTest for mcMain in the bell’s functions:
bellHolder.attachMovie('mcScoreAdd', 'txt'+this._name, bellHolder.getNextHighestDepth());//add some text to the stage
bellHolder['txt'+this._name]._x = this._x;//set the coordinates for the text
bellHolder['txt'+this._name]._y = this._y;
bellHolder['txt'+this._name].txtScore.text = _root.scoreInc;//set the text to the amount the score increased by
Do something similar for the bird:
bellHolder.attachMovie('mcScoreAdd', 'txt'+this._name, bellHolder.getNextHighestDepth());//add some text to the stage
bellHolder['txt'+this._name]._x = this._x;//set the coordinates for the text
bellHolder['txt'+this._name]._y = this._y;
bellHolder['txt'+this._name].txtScore.text = 'Double Score';//Show the user that the score was doubled
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 main onEnterFrame() 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:Number = 0;//how long the main guy has been falling down
Before we go any further, add the following code to the onEnterFrame() functions of both the bell and the bird:
if(_root.gameOver){
this.removeMovieClip();
}
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 ++;//increase the amount of time main has fallen
if(fallingTime >= 12){//if he has fallen long enough
gameOver = true;//then the game is over
}
}
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 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
}
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 showFinalStats function:
function showFinalStats():Void{
//attach the final screen to the stage:
attachMovie('mcFinalStats', 'finalstats', _root.getNextHighestDepth());
//setting the coordinates
finalstats._x = 115;
finalstats._y = 75;
finalstats.txtFinalScore.text = score;//showing the score
finalstats.btnPlayAgain.onRelease = function(){//when user clicks on the button
//reset everything
finalstats.removeMovieClip();
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!