Game Development Reference
In-Depth Information
After an enemy is attacked, its health is checked in the
checkHealth
method (see Listing 14-15).
Listing 14-15.
Enemy.js - Checking Health of the Enemy
p.checkHealth = function () {
var event;
if (this.healthBar.HP <= 0) {
this.destroy();
}
else {
event = new createjs.Event(events.ENEMY_ATTACKED_COMPLETE, true);
this.dispatchEvent(event);
}
}
p.destroy = function () {
var event;
this.enemySprite.on('animationend', function () {
event = new createjs.Event(events.ENEMY_DESTROYED, true);
this.dispatchEvent(event);
}, this);
this.enemySprite.gotoAndPlay(this.data.frame + '_die');
}
If the enemy still has hit points, the
ENEMY_ATTACKED_COMPLETE
event is dispatched back to the game, where it will
resume the battle. If the enemy's health bar is empty, it should die. Each enemy sprite has an animation sequence that
will dissolve it away with a pixelated effect. This animation is accessed by appending the string
_die
to the
frame
data
property. Once complete, the
ENEMY_DESTROYED
event is dispatched, and the current game state will be evaluated.
Listing 14-16 shows the entire
Enemy
class.
Listing 14-16.
Enemy.js - The Complete Enemy Class
(function () {
window.game = window.game || {}
function Enemy(type) {
this.data = data.EnemyData[type];
this.initialize();
}
var p = Enemy.prototype = new createjs.Container();
p.data = null;
p.enemySprite = null;
p.targetSprite = null;
p.magicSprite = null;
p.healthBar = null;
p.targetTween = null;
p.targetable = false;