November 2011
Intermediate to advanced
348 pages
7h 2m
English
In this recipe, we'll create a Health Bar class which is used to update and render the hero's health display.
Follow these steps to create a health bar class:
HealthBar constructor:/*
* HealthBar class should have no knowledge
* of the Actor or Level classes to
* keep it decoupled
*/
function HealthBar(config){
this.controller = config.controller;
this.maxHealth = config.maxHealth;
this.x = config.x;
this.y = config.y;
this.maxWidth = config.maxWidth;
this.height = config.height;
this.health = this.maxHealth;
}setHealth() method which sets the health value:HealthBar.prototype.setHealth = function(health){
this.health = health;
};draw() method which draws the health bar: ...Read now
Unlock full access