Game Development Reference
In-Depth Information
Listing 14-9. Enemy.js - The Enemy Class's Properties
(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;
p.Container_initialize = p.initialize;
p.initialize = function () {
this.Container_initialize();
this.createEnemySprite();
this.createTargetIndicator();
this.createHealthBar();
this.mouseEnabled = false;
}
//class methods here
window.game.Enemy = Enemy;
}());
The constructor function takes in one parameter, type , which is a string key from the level data when the level
is being built. This string is used to access the appropriate enemy data and set it to the data property. Next is a list of
properties used for the display objects in the class. These include the sprite for the enemy graphics, a sprite for a target
indicator to imply that the enemy is targetable, a sprite to display magic effects, and finally the health bar that displays
the current hit points of the enemy. The final two properties are used for targeting the enemy and will be discussed in
detail later in this section.
The initialize function does its typical series of method calls to initialize the class. The mouseEnabled property on
the container is initially set to false to prevent any interaction with the enemy. The list of initializing methods is seen
in Listing 14-10.
 
Search WWH ::




Custom Search