Game Development Reference
In-Depth Information
The class starts with some constants for an explosion event, the offset for the explosion sequence, and the time
the ship should be invincible after a new life starts. They are followed by some Boolean variables that determine if the
ship is invincible or if it should die in the next render cycle. Some speed and position values are also declared.
The
takeDamage
method is called when a bullet collides with it. This will play the sequence
"heroHit"
, which
will cause a white, glowing shimmer. The
explode
method will be called from the game in the render cycle, which
will play the explosion frames and an explosion sound. The registration points are adjusted so that the explosion will
be properly placed. A listener is set on this animation, which will fire
explosionComplete
to stop the animation and
dispatch the
EXPLOSION_COMPLETE
event.
The
reset
method resets
shouldDie
to
false
, puts the frame back on idle, and resets its registration points. Lastly,
the
makeInvincible
method prevents damage from being inflicted on the ship by setting
isInvincible
to true and
setting its alpha value to .4. The invincibility is reset with a
setTimeout
method using the
INVINCIBLE_TIME
constant.
Creating the Enemy Ships
There are two enemies in the game, each with their own damage animation frames. These ships will periodically
move down the screen and fire bullets that the hero must dodge. The frames that make up the enemy ships are
demonstrated in Figure
11-4
.
Figure 11-4.
The sprite frames for the enemy ships
Although there are two enemy ships, only one sprite class will be created. This class will randomly decide what
the ship's type should be, 1 or 2, and display the appropriate frame. The
type
property will also dictate the amount
of hit points it should have and the total points it will give when destroyed. The complete
EnemyShip
class is shown
in Listing 11-5.
Listing 11-5.
The EnemyShip Class, Declared in EnemyShip.js
(function (window) {
window.game = window.game || {}
function EnemyShip(startX) {
this.initialize(startX);
}
var p = EnemyShip.prototype = new createjs.Sprite();
p.Sprite_initialize = p.initialize;
p.type = null;
p.HP = null;
p.points = null;