HTML and CSS Reference
In-Depth Information
Writing the TileLayer Class
To add platformer support to the engine, add in another Quintus module: Quintus.Platformer . This
module adds in the Q.TileLayer class with a 2d component and a special stage optimized to work the
Q.TileLayer . The initial unoptimized TileLayer is straightforward. Its job is to load the tiles and draw
all the tiles on each frame.
Create a new JavaScript file called quintus_platformer.js , and add the code from Listing 18-1 .
Listing 18-1: The basic TileLayer class
Quintus.Platformer = function(Q) {
Q.TileLayer = Q.Sprite.extend({
init: function(props) {
this._super(_(props).defaults({
tileW: 32,
tileH: 32,
blockTileW: 10,
blockTileH: 10,
type: 1
}));
if(this.p.dataAsset) {
this.load(this.p.dataAsset);
}
this.blocks = [];
this.p.blockW = this.p.tileW * this.p.blockTileW;
this.p.blockH = this.p.tileH * this.p.blockTileH;
this.colBounds = {};
this.directions = [ 'top','left','right','bottom'];
},
load: function(dataAsset) {
var data = _.isString(dataAsset) ? Q.asset(dataAsset) :
dataAsset;
this.p.tiles = data;
this.p.rows = data.length;
this.p.cols = data[0].length;
this.p.w = this.p.rows * this.p.tileH;
this.p.h = this.p.cols * this.p.tileW;
},
setTile: function(x,y,tile) {
var p = this.p,
blockX = Math.floor(x/p.blockTileW),
blockY = Math.floor(y/p.blockTileH);
if(blockX >= 0 && blockY >= 0 &&
blockX < this.p.cols &&
 
 
Search WWH ::




Custom Search