Game Development Reference
In-Depth Information
Listing 14-10. Enemy.js - The Display Objects Created for the Enemy Class
p.createEnemySprite = function () {
this.enemySprite = new createjs.Sprite(spritesheet,
this.data.frame);
this.addChild(this.enemySprite);
}
p.createTargetIndicator = function () {
var bounds;
var targetYPos = 90;
var tweenSpeed = 700;
this.targetSprite = new createjs.Sprite(spritesheet, 'target');
bounds = this.targetSprite.getBounds();
this.targetSprite.regX = bounds.width / 2;
this.targetSprite.regY = bounds.height / 2;
this.targetSprite.y = targetYPos;
this.targetSprite.x = this.enemySprite.getBounds().width / 2;
this.targetTween = createjs.Tween
.get(this.targetSprite, {loop:true})
.to({alpha:.3}, tweenSpeed)
.to({alpha:1}, tweenSpeed);
this.targetTween.setPaused(true);
this.targetSprite.visible = false;
this.addChild(this.targetSprite);
}
p.createHealthBar = function () {
var enemyBounds = this.enemySprite.getBounds();
this.healthBar = new game.EnemyHealthBar(this.data.maxHP);
this.healthBar.y = enemyBounds.height + 10;
this.healthBar.x = (enemyBounds.width / 2) -
(this.healthBar.getBounds().width / 2);
this.addChild(this.healthBar);
}
The createEnemySprite method creates a new sprite using the appropriate frame by using the frame property
in the enemy data. Next, the target indicator sprite is created in the createTargetIndicator function and is centered
over the enemy sprite. A pulsing effect is added by creating a looped tween on the target. This tween is set to the class
property targetTween so you can start and stop it throughout the game. The tween is paused and its target sprite is set
to invisible.
The health bar for the enemy is next created and added to the container in the createHealthBar method.
As mentioned earlier, this health bar is another class, and its constructor function take only one parameter, which is
the amount of hit points the enemy should have. The bar is positioned and added to the enemy container. This health
bar class will be built in the next section, “Creating the Enemy Health Bar.” An enemy sprite and health bar are shown
in Figure 14-6 .
 
Search WWH ::




Custom Search