HTML and CSS Reference
In-Depth Information
5. We need to give the board an interval loop. Put the following tick event listener
before the end of the Board constructor funcion:
this.on('tick', this.tick);
6. We need a tick method in the board object to keep updaing the logic. The irst
thing we loop will assign the grid coordinate (row and column) to each sprite and
also update the 2D array to map the enemy:
// Tick Loop
Board.prototype.tick = function() {
if (cjs.Ticker.getPaused()) { return; }
// update the row/col for each enemy on the board
this.enemyMap = game.helper.create2DArray(this.cols, this.rows);
for (var i=0, len=this.enemyList.length; i<len; i++) {
var enemy = this.enemyList[i];
var rowCol = this.screenToRowCol(enemy.x, enemy.y);
// update both map and enemy's row/col
this.enemyMap[rowCol.col][rowCol.row] = enemy;
enemy.col = rowCol.col;
enemy.row = rowCol.row;
}
// check succeed enemies
// succeed enemies means it goes through the bottom area.
for(var i=0, len=this.enemyMap.length; i<len; i++) {
if (this.enemyMap[i][this.rows] !== undefined) { // found
enemy at the last row
var enemy = this.enemyMap[i][this.rows];
game.lives -= 1;
game.helper.removeItem(this.enemyList, enemy);
enemy.parent.removeChild(enemy);
}
}
};
7. The Board class manages the enemies. We add the following method to allow other
objects to add a new enemy through the board object:
Board.prototype.addEnemy = function(enemyClass) {
var sprite = new game[enemyClass]();
this.addChild(sprite);
var col = Math.floor(Math.random()*this.cols);// random col
 
Search WWH ::




Custom Search