HTML and CSS Reference
In-Depth Information
// list of buildings
this.buildingMap = game.helper.create2DArray(this.cols, this.
rows);
// event handling
game.on('readyToPlaceBuilding', this.readyToPlaceBuilding.
bind(this));
// mouse interaction
game.stage.on('stagemousemove', this.onMouseMove.bind(this));
game.stage.on('stagemouseup', this.onClick.bind(this));
}
4. The following two uility methods help in translaing the screen coordinate into
the board-grid coordinate:
// utilities
Board.prototype.screenToRowCol = function(x, y) {
var col = Math.floor(x / this.tileWidth);
var row = Math.floor(y / this.tileHeight);
return {col:col, row:row};
};
Board.prototype.rowColToScreen = function(row, col) {
var x = this.tileWidth * (col + 0.5); // +0.5 for tile center
var y = this.tileHeight * (row + 0.5);
return {x:x, y:y};
};
5. We have the following funcion to add a speciic type of building on a given
grid coordinate:
// Summon new piece on board
Board.prototype.addBuildingAtTile = function(buildingClass, col,
row) {
var sprite = new game[buildingClass]();
this.addChild(sprite);
var pos = this.rowColToScreen(row, col);
sprite.x = pos.x;
sprite.y = pos.y;
// store row/col for easy access later
sprite.row = row;
sprite.col = col;
this.buildingMap[col][row] = sprite;
};
 
Search WWH ::




Custom Search