Skip to content

Commit 30a0498

Browse files
committed
Working through blank tilemap fix.
1 parent ccffcbd commit 30a0498

1 file changed

Lines changed: 79 additions & 4 deletions

File tree

src/tilemap/Tilemap.js

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,31 @@ Phaser.Tilemap.TILED_JSON = 1;
166166
Phaser.Tilemap.prototype = {
167167

168168
/**
169-
* Creates an empty map of the given dimensions.
169+
* Creates an empty map of the given dimensions and one blank layer. If layers already exist they are erased.
170170
*
171171
* @method Phaser.Tilemap#create
172-
* @param {string} name - The name of the default layer of the map
172+
* @param {string} name - The name of the default layer of the map.
173173
* @param {number} width - The width of the map in tiles.
174174
* @param {number} height - The height of the map in tiles.
175175
* @param {number} tileWidth - The width of the tiles the map uses for calculations.
176176
* @param {number} tileHeight - The height of the tiles the map uses for calculations.
177+
* @param {Phaser.Group} [group] - Optional Group to add the layer to. If not specified it will be added to the World group.
178+
* @return {Phaser.TilemapLayer} The TilemapLayer object. This is an extension of Phaser.Image and can be moved around the display list accordingly.
177179
*/
178180
create: function (name, width, height, tileWidth, tileHeight) {
179181

182+
if (typeof group === 'undefined') { group = this.game.world; }
183+
180184
this.width = width;
181185
this.height = height;
182186

183187
this.setTileSize(tileWidth, tileHeight);
184188

189+
this.layers.length = 0;
190+
191+
return this.createBlankLayer(name, width, height, tileWidth, tileHeight, group);
192+
193+
/*
185194
var row;
186195
var output = [];
187196
@@ -217,6 +226,7 @@ Phaser.Tilemap.prototype = {
217226
});
218227
219228
this.currentLayer = this.layers.length - 1;
229+
*/
220230

221231
},
222232

@@ -422,6 +432,68 @@ Phaser.Tilemap.prototype = {
422432

423433
},
424434

435+
/**
436+
* Creates a new and empty layer on this Tilemap. By default TilemapLayers are fixed to the camera.
437+
*
438+
* @method Phaser.Tilemap#createLayer
439+
* @param {string} name - The name of this layer. Must be unique within the map.
440+
* @param {number} width - The width of the layer in tiles.
441+
* @param {number} height - The height of the layer in tiles.
442+
* @param {number} tileWidth - The width of the tiles the layer uses for calculations.
443+
* @param {number} tileHeight - The height of the tiles the layer uses for calculations.
444+
* @param {Phaser.Group} [group] - Optional Group to add the layer to. If not specified it will be added to the World group.
445+
* @return {Phaser.TilemapLayer} The TilemapLayer object. This is an extension of Phaser.Image and can be moved around the display list accordingly.
446+
*/
447+
createBlankLayer: function (name, width, height, tileWidth, tileHeight, group) {
448+
449+
if (typeof group === 'undefined') { group = this.game.world; }
450+
451+
if (this.getLayerIndex(layer) !== null)
452+
{
453+
console.warn('Tilemap.createBlankLayer: Layer with matching name already exists');
454+
return;
455+
}
456+
457+
var row;
458+
var output = [];
459+
460+
for (var y = 0; y < height; y++)
461+
{
462+
row = [];
463+
464+
for (var x = 0; x < width; x++)
465+
{
466+
row.push(null);
467+
}
468+
469+
output.push(row);
470+
}
471+
472+
this.layers.push({
473+
474+
name: name,
475+
x: 0,
476+
y: 0,
477+
width: width,
478+
height: height,
479+
widthInPixels: width * tileWidth,
480+
heightInPixels: height * tileHeight,
481+
alpha: 1,
482+
visible: true,
483+
properties: {},
484+
indexes: [],
485+
callbacks: [],
486+
bodies: [],
487+
data: output
488+
489+
});
490+
491+
this.currentLayer = this.layers.length - 1;
492+
493+
return group.add(new Phaser.TilemapLayer(this.game, this, this.layers.length - 1, width, height));
494+
495+
},
496+
425497
/**
426498
* Gets the layer index based on the layers name.
427499
*
@@ -938,7 +1010,9 @@ Phaser.Tilemap.prototype = {
9381010
*/
9391011
putTile: function (tile, x, y, layer) {
9401012

1013+
console.log('putTile' ,layer);
9411014
layer = this.getLayer(layer);
1015+
console.log('putTile2', layer);
9421016

9431017
if (x >= 0 && x < this.layers[layer].width && y >= 0 && y < this.layers[layer].height)
9441018
{
@@ -967,7 +1041,7 @@ Phaser.Tilemap.prototype = {
9671041
}
9681042
else
9691043
{
970-
this.layers[layer].data[y][x] = new Phaser.Tile(layer, index, x, y, this.tileWidth, this.tileHeight);
1044+
this.layers[layer].data[y][x] = new Phaser.Tile(this.layers[layer], index, x, y, this.tileWidth, this.tileHeight);
9711045
}
9721046
}
9731047

@@ -980,7 +1054,8 @@ Phaser.Tilemap.prototype = {
9801054
this.layers[layer].data[y][x].resetCollision();
9811055
}
9821056

983-
this.layers[layer].dirty = true;
1057+
this.layers[layer].dirty = true;
1058+
console.log(this.layers[layer]);
9841059

9851060
this.calculateFaces(layer);
9861061
}

0 commit comments

Comments
 (0)