Skip to content

Commit 1448562

Browse files
committed
Loader can now load JSON files specifically (game.load.json) and they are parsed and stored in the Game.Cache. Retrieve with game.cache.getJSON(key) (phaserjs#329)
Also fixed UTF encoding on the animation file.
1 parent fb5920f commit 1448562

5 files changed

Lines changed: 106 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ New features:
122122
* fixedToCamera now works across all display objects. When enabled it will fix at its current x/y coordinate, but can be changed via cameraOffset.
123123
* fixedToCamrea now works for Groups as well :) You can fix a Group to the camera and it will influence its children.
124124
* Tilemap.createCollisionObjects will parse Tiled data for objectgroups and convert polyline instances into physics objects you can collide with in the world.
125+
* Loader can now load JSON files specifically (game.load.json) and they are parsed and stored in the Game.Cache. Retrieve with game.cache.getJSON(key).
125126

126127

127128
Updates:
-39 KB
Binary file not shown.

src/loader/Cache.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ Phaser.Cache = function (game) {
4949
*/
5050
this._text = {};
5151

52+
/**
53+
* @property {object} _text - Text key-value container.
54+
* @private
55+
*/
56+
this._json = {};
57+
5258
/**
5359
* @property {object} _physics - Physics data key-value container.
5460
* @private
@@ -149,6 +155,12 @@ Phaser.Cache.BITMAPDATA = 9;
149155
*/
150156
Phaser.Cache.BITMAPFONT = 10;
151157

158+
/**
159+
* @constant
160+
* @type {number}
161+
*/
162+
Phaser.Cache.JSON = 11;
163+
152164
Phaser.Cache.prototype = {
153165

154166
/**
@@ -382,6 +394,20 @@ Phaser.Cache.prototype = {
382394

383395
},
384396

397+
/**
398+
* Add a new json object into the cache.
399+
*
400+
* @method Phaser.Cache#addJSON
401+
* @param {string} key - Asset key for the text data.
402+
* @param {string} url - URL of this text data file.
403+
* @param {object} data - Extra text data.
404+
*/
405+
addJSON: function (key, url, data) {
406+
407+
this._json[key] = { url: url, data: data };
408+
409+
},
410+
385411
/**
386412
* Add a new image.
387413
*
@@ -880,6 +906,26 @@ Phaser.Cache.prototype = {
880906

881907
},
882908

909+
/**
910+
* Get a JSON object by key from the cache.
911+
*
912+
* @method Phaser.Cache#getJSON
913+
* @param {string} key - Asset key of the json object to retrieve from the Cache.
914+
* @return {object} The JSON object.
915+
*/
916+
getJSON: function (key) {
917+
918+
if (this._json[key])
919+
{
920+
return this._json[key].data;
921+
}
922+
else
923+
{
924+
console.warn('Phaser.Cache.getJSON: Invalid key: "' + key + '"');
925+
}
926+
927+
},
928+
883929
/**
884930
* Get binary data by key.
885931
*
@@ -952,6 +998,10 @@ Phaser.Cache.prototype = {
952998
case Phaser.Cache.BITMAPFONT:
953999
array = this._bitmapFont;
9541000
break;
1001+
1002+
case Phaser.Cache.JSON:
1003+
array = this._json;
1004+
break;
9551005
}
9561006

9571007
if (!array)
@@ -1013,6 +1063,16 @@ Phaser.Cache.prototype = {
10131063
delete this._text[key];
10141064
},
10151065

1066+
/**
1067+
* Removes a json object from the cache.
1068+
*
1069+
* @method Phaser.Cache#removeJSON
1070+
* @param {string} key - Key of the asset you want to remove.
1071+
*/
1072+
removeJSON: function (key) {
1073+
delete this._json[key];
1074+
},
1075+
10161076
/**
10171077
* Removes a physics data file from the cache.
10181078
*
@@ -1090,6 +1150,11 @@ Phaser.Cache.prototype = {
10901150
delete this._text[item['key']];
10911151
}
10921152

1153+
for (var item in this._json)
1154+
{
1155+
delete this._json[item['key']];
1156+
}
1157+
10931158
for (var item in this._textures)
10941159
{
10951160
delete this._textures[item['key']];

src/loader/Loader.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,32 @@ Phaser.Loader.prototype = {
361361

362362
},
363363

364+
/**
365+
* Add a json file to the Loader.
366+
*
367+
* @method Phaser.Loader#json
368+
* @param {string} key - Unique asset key of the json file.
369+
* @param {string} url - URL of the json file.
370+
* @param {boolean} [overwrite=false] - If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.
371+
* @return {Phaser.Loader} This Loader instance.
372+
*/
373+
json: function (key, url, overwrite) {
374+
375+
if (typeof overwrite === "undefined") { overwrite = false; }
376+
377+
if (overwrite)
378+
{
379+
this.replaceInFileList('json', key, url);
380+
}
381+
else
382+
{
383+
this.addToFileList('json', key, url);
384+
}
385+
386+
return this;
387+
388+
},
389+
364390
/**
365391
* Add a JavaScript file to the Loader. Once loaded the JavaScript file will be automatically turned into a script tag (and executed), so be careful what you load!
366392
*
@@ -885,6 +911,15 @@ Phaser.Loader.prototype = {
885911

886912
break;
887913

914+
case 'json':
915+
this._xhr.open("GET", this.baseURL + file.url, true);
916+
this._xhr.responseType = "text";
917+
this._xhr.onload = function () {
918+
return _this.jsonLoadComplete(_this._fileIndex);
919+
};
920+
this._xhr.send();
921+
break;
922+
888923
case 'tilemap':
889924
this._xhr.open("GET", this.baseURL + file.url, true);
890925
this._xhr.responseType = "text";
@@ -1176,6 +1211,10 @@ Phaser.Loader.prototype = {
11761211
{
11771212
this.game.cache.addTilemap(file.key, file.url, data, file.format);
11781213
}
1214+
else if (file.type === 'json')
1215+
{
1216+
this.game.cache.addJSON(file.key, file.url, data);
1217+
}
11791218
else
11801219
{
11811220
this.game.cache.addTextureAtlas(file.key, file.url, file.data, data, file.format);

tutorials/01 Getting Started/part4.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ <h3>Sublime Text</h3>
2929

3030
<h3>WebStorm</h3>
3131

32-
<p>JetBrains WebStorm is an extremely advanced fully development environment. It goes well beyond simple code editing and offers all of the high-level features you'd expect from a proper IDE include code-insight, npm built-in, code style/syntax reports, source control and a wealth of other features designed more for web developers than game developers. It's based on IntelliJ IDEA, which is both a good and bad thing. For a start the actual code editing experience is nothing like as smooth and freeform as with Sublime, but the power features can often make up for that. Having errors with your code spotted for you, before you've even tested your game can be really useful. And code-completion is great too, although obviously somewhat limited by the myriad ways JavaScript can be written.</p>
32+
<p>JetBrains WebStorm is an extremely advanced fully development environment. It goes well beyond simple code editing and offers all of the high-level features you'd expect from a proper IDE include code-insight, npm built-in, code style/syntax reports, source control and a wealth of other features designed more for web developers than game developers. It's based on IntelliJ IDEA, a heavily Java based editor, which is both a good and bad thing. For a start the actual code editing experience is nothing like as smooth and freeform as with Sublime and OS integration is weak, but the power features can often make up for that. Having errors with your code spotted for you, before you've even tested your game can be really useful. And code-completion is great too, although obviously somewhat limited by the myriad ways JavaScript can be written.</p>
3333

3434
<p>The full version starts from $49 and is available for Windows and OS X. There are often deals to be found on the JetBrains site too.</p>
3535

0 commit comments

Comments
 (0)