Game Development Reference
In-Depth Information
p.lastFired = 0;
p.fireDelay = 2000;
p.speed = 150;
p.nextY = 0;
p.shouldDie = false;
p.initialize = function (startX) {
this.type = Utils.getRandomNumber(0, 2) + 1;
this.HP = this.type * 3;
this.points = this.type * 100;
this.Sprite_initialize(spritesheet, "enemy" + this.type + "Idle");
this.regX = this.getBounds().width / 2;
this.regY = this.getBounds().height / 2;
}
p.takeDamage = function () {
this.gotoAndPlay("enemy" + this.type + "Hit");
this.HP--;
if (this.HP <= 0) {
this.shouldDie = true;
}
}
p.reset = function () {
this.type = Utils.getRandomNumber(0, 2) + 1;
this.shouldDie = false;
this.HP = this.type * 3;
this.points = this.type * 100;
this.gotoAndPlay("enemy" + this.type + "Idle");
}
window.game.EnemyShip = EnemyShip;
}(window));
The
type
,
HP
, and
points
properties are crucial variables for the behavior of each ship. The type, which will be
randomly assigned in the
initialize
function, will decide the frame, hit points, and worth of each ship sprite. The
next set is used for firing bullets. The
lastFired
variable holds the time when the last enemy bullet was fired, and
fireDelay
is the time that should pass before the next attack. The
speed
variable is used to determine how fast it
should move down the stage, and
nextY
is set and used in the update/render cycle during the game loop. Finally,
shouldDie
is used to determine if the ship should blow up during the next render cycle.
Next is a small series of methods. The initialize function randomly determines what type of ship the enemy
should be and adjusts the appropriate properties. When the ship is hit, its
takeDamage
function will immediately play
either the
enemy1Hit
or
enemy2Hit
animations, depending on the ship's type. Its
HP
value is decreased by one and is
followed by a conditional to check if the ship should die. If so, the
shouldDie
property is set to true. This property will
be examined during the render cycle in the game loop.
The final function,
reset
, will reset its properties by choosing a new type. This might seem strange now, but
these ship sprites will be recycled and served up from an
object pool.
This technique will be looked at in detail in the
upcoming “Using Object Pools” section.