HTML and CSS Reference
In-Depth Information
2. In the Castle constructor, we add the tick listener:
this.ticks = 0;
this.on('tick', this.tick);
3. The tick listener is used to summon the bullets. When the value of the tick
listener matches the atacking frequency, Castle fires the bullet:
Castle.prototype.tick = function() {
if (cjs.Ticker.getPaused()) { return; }
this.ticks += 1;
// summon bullet every once in a while
if (this.ticks % this.attackSpeed === 0) {
this.summonBullet();
}
};
4. We simply need to put the bullet on the board:
Castle.prototype.summonBullet = function() {
var bullet = new game.Bullet(this.damageDeal);
bullet.x = this.x + Math.random()*20 - 10;
bullet.y = this.y;
this.parent.addBullet(bullet);
};
5. The Castle class triggers the bullet creaion logic, but we manage it with the
board object. This makes sense because board is the container of all the game
objects. Let's move to the board.js file. We maintain a list of all the bullets with
bulletList . Add the following declaraion to the Board class constructor:
// bullet list for centralized collision detection
this.bulletList = [];
6. Then, we need a method for the castle to pass the bullet reference to the board.
With this reference, we add the bullet into effectLayer and the array:
Board.prototype.addBullet = function(bullet){
game.effectLayer.addChild(bullet);
this.bulletList.push(bullet);
};
7. In the board's ick, we check whether the bullets hit the enemy. The hit is based on
the grid coordinate:
// check bullet collision
// loop from top because we remove item inside loop
 
Search WWH ::




Custom Search