Skip to content

Instantly share code, notes, and snippets.

@casarock
Last active August 29, 2015 13:56
Show Gist options
  • Save casarock/9078481 to your computer and use it in GitHub Desktop.
Save casarock/9078481 to your computer and use it in GitHub Desktop.
load dynamic tilemaps.
var demo = {
getTiledMapDefinition: function(tilemap, layerName, tileWidth, tileHeight, tileImageName, imgWidth, imgHeight) {
var tileData = this.tileMapToArray(tilemap),
tilemapHeight = tilemap.length,
tilemapWidth = tilemap[0].length,
tileLayer = this.tiledLayerBlueprint(tileData, tilemapHeight, tilemapWidth, layerName),
tileImage = this.tiledTilesBlueprint(imgWidth, imgHeight, tileImageName, tileWidth, tileHeight);
return this.tiledBlueprint(tileLayer, tileImage);
},
tiledBlueprint: function(layers, tilesets) {
return {
"height": 0,
"width": 0,
"layers": (typeof layers === 'array') ? layers : [layers],
"orientation": "orthogonal",
"tileheight": 0,
"tilewidth": 0,
"tilesets": (typeof tilesets === 'array') ? tilesets : [tilesets],
"properties": {},
"version": 1
};
},
tiledLayerBlueprint: function(data, height, width, name) {
return {
"data": data,
"height": height,
"name": name,
"opacity": 1,
"type": "tilelayer",
"visible": true,
"width": width,
"x": 0,
"y": 0
};
},
tiledTilesBlueprint: function(imgWidth, imgHeight, name, tilewidth, tileheight) {
return {
"firstgid": 0,
"image": "",
"imageheight": imgHeight,
"imagewidth": imgWidth,
"margin": 1,
"name": name,
"spacing": 0,
"tileheight": tileheight || 32,
"tilewidth": tilewidth || 32,
"properties": {}
};
}
}
var tilemap = getSomeTilemap(w, h);
var tiles = demo.getTiledMapDefinition(tilemap, ...)
this.game.load.tilemap('map', null, tiles, Phaser.Tilemap.TILED_JSON);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment