HTML and CSS Reference
In-Depth Information
EnemyMissile.prototype.step = function(dt) {
this.y += this.vy * dt;
var collision = this.board.collide(this,OBJECT_PLAYER)
if(collision) {
collision.hit(this.damage);
this.board.remove(this);
} else if(this.y > Game.height) {
this.board.remove(this);
}
};
EnemyMissile is much like the evil twin to PlayerMisisle . It has a different vertical direction, a dif-
ferent type, a different type to collide against, and a different check for when it's off the board. The functionality
is all the same; it's just doing it in reverse.
To get EnemyMissile objects onto the page, the Enemy step function needs to fire some off at some ran-
dom interval. As an added complication, some enemies can fire two missiles at a time, much like the player, and
some can fire just one, straight down the center.
The sprite enemy_missile also needs to be defined, so add this entry to the sprites list at the top of
game.js :
var sprites = {
ship: { sx: 0, sy: 0, w: 37, h: 42, frames: 1 },
missile: { sx: 0, sy: 30, w: 2, h: 10, frames: 1 },
enemy_purple: { sx: 37, sy: 0, w: 42, h: 43, frames: 1 },
enemy_bee: { sx: 79, sy: 0, w: 37, h: 43, frames: 1 },
enemy_ship: { sx: 116, sy: 0, w: 42, h: 43, frames: 1 },
enemy_circle: { sx: 158, sy: 0, w: 32, h: 33, frames: 1 },
explosion: { sx: 0, sy: 64, w: 64, h: 64, frames: 12 } ,
enemy_missile: { sx: 9, sy: 42, w: 3, h: 20, frame: 1 }
};
Modify the Enemy object as highlighted here to add in missile firing capabilities:
Enemy.prototype = new Sprite();
Enemy.prototype.type = OBJECT_ENEMY;
Enemy.prototype.baseParameters =
{ A: 0, B: 0, C: 0, D: 0,
E: 0, F: 0, G: 0, H: 0,
t: 0, firePercentage: 0.01,
reloadTime: 0.75, reload: 0 };
Enemy.prototype.step = function(dt) {
this.t += dt;
this.vx = this.A +
this.B * Math.sin(this.C * this.t + this.D);
this.vy = this.E +
this.F * Math.sin(this.G * this.t + this.H);
Search WWH ::




Custom Search