Game Development Reference
In-Depth Information
p.Container_initialize = p.initialize;
p.initialize = function () {
this.Container_initialize();
this.x = screen_width - 165;
this.y = 5;
this.updateScore(0);
}
p.updateScore = function(points){
var formattedScore;
this.removeAllChildren();
this.score += points;
formattedScore = this.addLeadingZeros(this.score, 7);
this.scoreTxt = new createjs.BitmapText(formattedScore, spritesheet);
this.addChild(this.scoreTxt);
}
p.addLeadingZeros = function (score, width) {
score = score + '';
return score.length >= width ? score : new Array(width -
score.length + 1).join(0) + score;
}
p.getScore = function () {
return this.addLeadingZeros(this.score, 7);
}
window.game.Scoreboard = Scoreboard;
}(window));
The Scoreboard class holds two properties, the score and the score BitmapText display object. When the
scoreboard should be updated, the updateScore method is called, which accepts the number of points that the score
should be increased by. Since a new bitmap text object needs to be created to update the message, the old one is
removed by removing all of the children in the container. The new score is then updated and passed through the
addLeadingZeros method. This function takes the new score and adds the appropriate number of leading zeros so
that the total length of the score text is always 7. This method takes two parameters, the score and the number of
characters the returned string should be. For example, if the current score were 1600, the returned value from the
addLeadingZeros method would be 0001600. This is primarily for effect and to mimic the look of a classic score board
(see Figure 11-8 ).
Figure 11-8. The Scoreboard HUD element
A simple method is then added to the class to get the current score. This is used later in the game when the score
needs to be accessed for messaging in the game over screen.
 
Search WWH ::




Custom Search