Skip to content

Commit 8da9b67

Browse files
committed
Loader fixed for tilemap data.
1 parent 780b8a5 commit 8da9b67

3 files changed

Lines changed: 119 additions & 18 deletions

File tree

examples/_site/examples.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,18 @@
181181
{
182182
"file": "fire.js",
183183
"title": "fire"
184+
},
185+
{
186+
"file": "lightbeams.js",
187+
"title": "lightbeams"
188+
},
189+
{
190+
"file": "plasma.js",
191+
"title": "plasma"
192+
},
193+
{
194+
"file": "tunnel.js",
195+
"title": "tunnel"
184196
}
185197
],
186198
"games": [
@@ -416,6 +428,10 @@
416428
"file": "load+texture+atlas.js",
417429
"title": "load texture atlas"
418430
},
431+
{
432+
"file": "load+tilemap+json.js",
433+
"title": "load tilemap json"
434+
},
419435
{
420436
"file": "pick+images+from+cache.js",
421437
"title": "pick images from cache"
@@ -608,10 +624,6 @@
608624
"file": "animated+tiling+sprite.js",
609625
"title": "animated tiling sprite"
610626
},
611-
{
612-
"file": "tiling+sprite+input.js",
613-
"title": "tiling sprite input"
614-
},
615627
{
616628
"file": "tiling+sprite.js",
617629
"title": "tiling sprite"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });
3+
4+
function preload() {
5+
6+
7+
// Tilemaps are split into two parts: The actual map data (usually stored in a CSV or JSON file)
8+
// and the tileset/s used to render the map.
9+
10+
// Here we'll load the tilemap data. The first parameter is a unique key for the map data.
11+
12+
// The second is a URL to the JSON file the map data is stored in. This is actually optional, you can pass the JSON object as the 3rd
13+
// parameter if you already have it loaded (maybe via a 3rd party source or pre-generated). In which case pass 'null' as the URL and
14+
// the JSON object as the 3rd parameter.
15+
16+
// The final one tells Phaser the foramt of the map data, in this case it's a JSON file exported from the Tiled map editor.
17+
// This could be Phaser.Tilemap.CSV too.
18+
19+
game.load.tilemap('mario', 'assets/maps/mario1.json', null, Phaser.Tilemap.TILED_JSON);
20+
21+
// Next we load the tileset. This consists of an image and a set of values that determine the size of the tiles within the image.
22+
// In this case we give it a unique key, the URL to the PNG file and tell Phaser the tiles are all 16x16 pixels in size.
23+
24+
game.load.tileset('tiles', 'assets/maps/mario1.png', 16, 16);
25+
26+
}
27+
28+
var map;
29+
var tileset;
30+
var layer;
31+
32+
function create() {
33+
34+
game.stage.backgroundColor = '#787878';
35+
36+
map = game.add.tilemap('mario');
37+
38+
tileset = game.add.tileset('tiles');
39+
40+
layer = game.add.tilemapLayer(0, 100, 800, 600, tileset, map, 0);
41+
42+
layer.fixedToCamera = false;
43+
44+
// layer.position.y = 200;
45+
46+
layer.resizeWorld();
47+
48+
}
49+
50+
function update() {
51+
52+
layer.scrollX--;
53+
54+
}

src/loader/Loader.js

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ Phaser.Loader.prototype = {
302302
* @param {string} key - Unique asset key of this image file.
303303
* @param {string} url - URL of image file.
304304
* @param {boolean} [overwrite=false] - If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.
305+
* @return {Phaser.Loader} This Loader instance.
305306
*/
306307
image: function (key, url, overwrite) {
307308

@@ -327,6 +328,7 @@ Phaser.Loader.prototype = {
327328
* @param {string} key - Unique asset key of the text file.
328329
* @param {string} url - URL of the text file.
329330
* @param {boolean} [overwrite=false] - If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.
331+
* @return {Phaser.Loader} This Loader instance.
330332
*/
331333
text: function (key, url, overwrite) {
332334

@@ -351,6 +353,7 @@ Phaser.Loader.prototype = {
351353
* @method Phaser.Loader#script
352354
* @param {string} key - Unique asset key of the script file.
353355
* @param {string} url - URL of the JavaScript file.
356+
* @return {Phaser.Loader} This Loader instance.
354357
*/
355358
script: function (key, url) {
356359

@@ -369,6 +372,7 @@ Phaser.Loader.prototype = {
369372
* @param {number} frameWidth - Width of each single frame.
370373
* @param {number} frameHeight - Height of each single frame.
371374
* @param {number} [frameMax=-1] - How many frames in this sprite sheet. If not specified it will divide the whole image into frames.
375+
* @return {Phaser.Loader} This Loader instance.
372376
*/
373377
spritesheet: function (key, url, frameWidth, frameHeight, frameMax) {
374378

@@ -391,6 +395,7 @@ Phaser.Loader.prototype = {
391395
* @param {number} [tileMax=-1] - How many tiles in this tileset. If not specified it will divide the whole image into tiles.
392396
* @param {number} [tileMargin=0] - If the tiles have been drawn with a margin, specify the amount here.
393397
* @param {number} [tileSpacing=0] - If the tiles have been drawn with spacing between them, specify the amount here.
398+
* @return {Phaser.Loader} This Loader instance.
394399
*/
395400
tileset: function (key, url, tileWidth, tileHeight, tileMax, tileMargin, tileSpacing) {
396401

@@ -411,6 +416,7 @@ Phaser.Loader.prototype = {
411416
* @param {string} key - Unique asset key of the audio file.
412417
* @param {Array|string} urls - An array containing the URLs of the audio files, i.e.: [ 'jump.mp3', 'jump.ogg', 'jump.m4a' ] or a single string containing just one URL.
413418
* @param {boolean} autoDecode - When using Web Audio the audio files can either be decoded at load time or run-time. They can't be played until they are decoded, but this let's you control when that happens. Decoding is a non-blocking async process.
419+
* @return {Phaser.Loader} This Loader instance.
414420
*/
415421
audio: function (key, urls, autoDecode) {
416422

@@ -427,10 +433,10 @@ Phaser.Loader.prototype = {
427433
*
428434
* @method Phaser.Loader#tilemap
429435
* @param {string} key - Unique asset key of the tilemap data.
430-
* @param {string} tilesetURL - The url of the tile set image file.
431436
* @param {string} [mapDataURL] - The url of the map data file (csv/json)
432-
* @param {object} [mapData] - An optional JSON data object (can be given in place of a URL).
433-
* @param {string} [format] - The format of the map data.
437+
* @param {object} [mapData] - An optional JSON data object. If given then the mapDataURL is ignored and this JSON object is used for map data instead.
438+
* @param {string} [format=Phaser.Tilemap.CSV] - The format of the map data. Either Phaser.Tilemap.CSV or Phaser.Tilemap.TILED_JSON.
439+
* @return {Phaser.Loader} This Loader instance.
434440
*/
435441
tilemap: function (key, mapDataURL, mapData, format) {
436442

@@ -445,12 +451,8 @@ Phaser.Loader.prototype = {
445451
return this;
446452
}
447453

448-
// A URL to a json/csv file has been given
449-
if (mapDataURL)
450-
{
451-
this.addToFileList('tilemap', key, mapDataURL, { format: format });
452-
}
453-
else
454+
// A map data object has been given
455+
if (mapData)
454456
{
455457
switch (format)
456458
{
@@ -470,6 +472,10 @@ Phaser.Loader.prototype = {
470472

471473
this.game.cache.addTilemap(key, null, mapData, format);
472474
}
475+
else
476+
{
477+
this.addToFileList('tilemap', key, mapDataURL, { format: format });
478+
}
473479

474480
return this;
475481

@@ -483,6 +489,7 @@ Phaser.Loader.prototype = {
483489
* @param {string} textureURL - The url of the font image file.
484490
* @param {string} [xmlURL] - The url of the font data file (xml/fnt)
485491
* @param {object} [xmlData] - An optional XML data object.
492+
* @return {Phaser.Loader} This Loader instance.
486493
*/
487494
bitmapFont: function (key, textureURL, xmlURL, xmlData) {
488495

@@ -542,6 +549,7 @@ Phaser.Loader.prototype = {
542549
* @param {string} textureURL - The url of the texture atlas image file.
543550
* @param {string} [atlasURL] - The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.
544551
* @param {object} [atlasData] - A JSON or XML data object. You don't need this if the data is being loaded from a URL.
552+
* @return {Phaser.Loader} This Loader instance.
545553
*/
546554
atlasJSONArray: function (key, textureURL, atlasURL, atlasData) {
547555

@@ -557,6 +565,7 @@ Phaser.Loader.prototype = {
557565
* @param {string} textureURL - The url of the texture atlas image file.
558566
* @param {string} [atlasURL] - The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.
559567
* @param {object} [atlasData] - A JSON or XML data object. You don't need this if the data is being loaded from a URL.
568+
* @return {Phaser.Loader} This Loader instance.
560569
*/
561570
atlasJSONHash: function (key, textureURL, atlasURL, atlasData) {
562571

@@ -572,6 +581,7 @@ Phaser.Loader.prototype = {
572581
* @param {string} textureURL - The url of the texture atlas image file.
573582
* @param {string} [atlasURL] - The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.
574583
* @param {object} [atlasData] - A JSON or XML data object. You don't need this if the data is being loaded from a URL.
584+
* @return {Phaser.Loader} This Loader instance.
575585
*/
576586
atlasXML: function (key, textureURL, atlasURL, atlasData) {
577587

@@ -588,6 +598,7 @@ Phaser.Loader.prototype = {
588598
* @param {string} [atlasURL] - The url of the texture atlas data file (json/xml). You don't need this if you are passing an atlasData object instead.
589599
* @param {object} [atlasData] - A JSON or XML data object. You don't need this if the data is being loaded from a URL.
590600
* @param {number} [format] - A value describing the format of the data, the default is Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY.
601+
* @return {Phaser.Loader} This Loader instance.
591602
*/
592603
atlas: function (key, textureURL, atlasURL, atlasData, format) {
593604

@@ -728,6 +739,12 @@ Phaser.Loader.prototype = {
728739
*/
729740
loadFile: function () {
730741

742+
if (!this._fileList[this._fileIndex])
743+
{
744+
console.warn('Phaser.Loader loadFile invalid index ' + this._fileIndex);
745+
return;
746+
}
747+
731748
var file = this._fileList[this._fileIndex];
732749
var _this = this;
733750

@@ -805,13 +822,13 @@ Phaser.Loader.prototype = {
805822
this._xhr.open("GET", this.baseURL + file.url, true);
806823
this._xhr.responseType = "text";
807824

808-
if (file.format == Phaser.Tilemap.TILED_JSON)
825+
if (file.format === Phaser.Tilemap.TILED_JSON)
809826
{
810827
this._xhr.onload = function () {
811828
return _this.jsonLoadComplete(_this._fileIndex);
812829
};
813830
}
814-
else if (file.format == Phaser.Tilemap.CSV)
831+
else if (file.format === Phaser.Tilemap.CSV)
815832
{
816833
this._xhr.onload = function () {
817834
return _this.csvLoadComplete(_this._fileIndex);
@@ -1061,10 +1078,18 @@ Phaser.Loader.prototype = {
10611078
*/
10621079
jsonLoadComplete: function (index) {
10631080

1064-
var data = JSON.parse(this._xhr.responseText);
1081+
if (!this._fileList[index])
1082+
{
1083+
console.warn('Phaser.Loader jsonLoadComplete invalid index ' + index);
1084+
return;
1085+
}
1086+
10651087
var file = this._fileList[index];
1088+
var data = JSON.parse(this._xhr.responseText);
1089+
1090+
file.loaded = true;
10661091

1067-
if (file.type == 'tilemap')
1092+
if (file.type === 'tilemap')
10681093
{
10691094
this.game.cache.addTilemap(file.key, file.url, data, file.format);
10701095
}
@@ -1085,8 +1110,16 @@ Phaser.Loader.prototype = {
10851110
*/
10861111
csvLoadComplete: function (index) {
10871112

1088-
var data = this._xhr.responseText;
1113+
if (!this._fileList[index])
1114+
{
1115+
console.warn('Phaser.Loader csvLoadComplete invalid index ' + index);
1116+
return;
1117+
}
1118+
10891119
var file = this._fileList[index];
1120+
var data = this._xhr.responseText;
1121+
1122+
file.loaded = true;
10901123

10911124
this.game.cache.addTilemap(file.key, file.url, data, file.format);
10921125

@@ -1104,6 +1137,7 @@ Phaser.Loader.prototype = {
11041137

11051138
var file = this._fileList[index];
11061139

1140+
file.loaded = true;
11071141
file.error = true;
11081142

11091143
console.warn("Phaser.Loader dataLoadError: " + file.key);
@@ -1148,6 +1182,7 @@ Phaser.Loader.prototype = {
11481182
}
11491183

11501184
var file = this._fileList[index];
1185+
file.loaded = true;
11511186

11521187
if (file.type == 'bitmapfont')
11531188
{

0 commit comments

Comments
 (0)