Game Development Reference
In-Depth Information
Now you define a ScoreGameObject class that inherits from the Label class. The score is represented
by the text content. The nice thing is that JavaScript will automatically transform an integer number
into text when drawing. So, you simply set the text to 0:
function ScoreGameObject(fontName, fontSize, layer, id) {
Label.call(this, fontName, fontSize, layer, id);
this.text = 0;
this._align = "right";
}
ScoreGameObject.prototype = Object.create(Label.prototype);
You also add a score property that lets you retrieve or modify the current score:
Object.defineProperty(ScoreGameObject.prototype, "score",
{
get: function () {
return this._contents;
},
set: function (value) {
if (value >=0)
this.text = value;
}
});
Finally, you add a reset method, so that when the game is over, you can call this method to reset the
score to zero:
ScoreGameObject.prototype.reset = function () {
this.text = 0;
};
Now that you have this class, you can simply create an instance of it and add it to the game world.
Place these instructions in the JewelJamGameWorld class, and add a frame overlay on which you can
draw the current score. These instructions do all the work:
var scoreFrame = new SpriteGameObject(sprites.frame_score, ID.layer_overlays);
scoreFrame.position = new Vector2(20, 20);
this.add(scoreFrame);
var score = new ScoreGameObject("Segoe UI Mono", "40px", ID.layer_overlays_1,
ID.score);
score.position = new Vector2(270, 35);
score.color = Color.white;
this.add(score);
Search WWH ::




Custom Search