Skip to content

Commit c154b8c

Browse files
committed
Loader.resetLocked is a boolean that allows you to control what happens when the loader is reset, *which happens automatically on a State change*. If you set resetLocked to true it allows you to populate the loader queue in one State, then swap to another State without having the queue erased, and start the load going from there. After the load has completed you could then disable the lock again as needed.
Loader.reset has a new optional 2nd parameter `clearEvents` which if set to `true` (the default is false) will reset all event listeners bound to the Loader.
1 parent ec732e4 commit c154b8c

2 files changed

Lines changed: 40 additions & 6 deletions

File tree

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ The URL is always transformed through transformUrl, which can make adding some o
8080

8181
This also incorporates the fast-cache path for Images tags that can greatly speed up the responsiveness of image loading.
8282

83-
Thanks to @pnstickne for this update.
83+
Loader.resetLocked is a boolean that allows you to control what happens when the loader is reset, *which happens automatically on a State change*. If you set `resetLocked` to `true` it allows you to populate the loader queue in one State, then swap to another State without having the queue erased, and start the load going from there. After the load has completed you could then disable the lock again as needed.
84+
85+
Thanks to @pnstickne for vast majority of this update.
8486

8587
### New Features
8688

@@ -113,6 +115,7 @@ Thanks to @pnstickne for this update.
113115
* Body.reset now resets the Body.speed value to zero.
114116
* Device.touch checks if `window.navigator.maxTouchPoints` is `>= 1` rather than > 1, which now allows touch events to work properly in Chrome mobile emulation.
115117
* Loader.XDomainRequest wasn't used for atlas json loading. It has now been moved to the `xhrLoad` method to ensure it's used for all request if required (thanks @draconisNoctis #1601)
118+
* Loader.reset has a new optional 2nd parameter `clearEvents` which if set to `true` (the default is false) will reset all event listeners bound to the Loader.
116119

117120
### Bug Fixes
118121

src/loader/Loader.js

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ Phaser.Loader = function (game) {
2828
*/
2929
this.game = game;
3030

31+
/**
32+
* If true all calls to Loader.reset will be ignored. Useful if you need to create a load queue before swapping to a preloader state.
33+
* @property {boolean} resetLocked
34+
* @default
35+
*/
36+
this.resetLocked = false;
37+
3138
/**
3239
* True if the Loader is in the process of loading the queue.
3340
* @property {boolean} isLoading
@@ -396,15 +403,25 @@ Phaser.Loader.prototype = {
396403
},
397404

398405
/**
399-
* Reset the loader and clear any queued assets.
406+
* Reset the loader and clear any queued assets. If `Loader.resetLocked` is true this operation will abort.
400407
*
401408
* This will abort any loading and clear any queued assets.
402409
*
410+
* Optionally you can clear any associated events.
411+
*
403412
* @method Phaser.Loader#reset
404413
* @protected
405414
* @param {boolean} [hard=false] - If true then the preload sprite and other artifacts may also be cleared.
415+
* @param {boolean} [clearEvents=false] - If true then the all Loader signals will have removeAll called on them.
406416
*/
407-
reset: function (hard) {
417+
reset: function (hard, clearEvents) {
418+
419+
if (typeof clearEvents === 'undefined') { clearEvents = false; }
420+
421+
if (this.resetLocked)
422+
{
423+
return;
424+
}
408425

409426
if (hard)
410427
{
@@ -423,6 +440,16 @@ Phaser.Loader.prototype = {
423440
this._loadedPackCount = 0;
424441
this._loadedFileCount = 0;
425442

443+
if (clearEvents)
444+
{
445+
this.onLoadStart.removeAll();
446+
this.onLoadComplete.removeAll();
447+
this.onPackComplete.removeAll();
448+
this.onFileStart.removeAll();
449+
this.onFileComplete.removeAll();
450+
this.onFileError.removeAll();
451+
}
452+
426453
},
427454

428455
/**
@@ -757,14 +784,14 @@ Phaser.Loader.prototype = {
757784
* @method Phaser.Loader#audiosprite
758785
* @param {string} key - Unique asset key of the audio file.
759786
* @param {Array|string} urls - An array containing the URLs of the audio files, i.e.: [ 'audiosprite.mp3', 'audiosprite.ogg', 'audiosprite.m4a' ] or a single string containing just one URL.
760-
* @param {string} atlasURL - The URL of the audiosprite configuration json.
787+
* @param {string} jsonURL - The URL of the audiosprite configuration json.
761788
* @return {Phaser.Loader} This Loader instance.
762789
*/
763-
audiosprite: function(key, urls, atlasURL) {
790+
audiosprite: function(key, urls, jsonURL) {
764791

765792
this.audio(key, urls);
766793

767-
this.json(key + '-audioatlas', atlasURL);
794+
this.json(key + '-audioatlas', jsonURL);
768795

769796
return this;
770797

@@ -1398,6 +1425,10 @@ Phaser.Loader.prototype = {
13981425
this.audio(file.key, file.urls, file.autoDecode);
13991426
break;
14001427

1428+
case "audiosprite":
1429+
this.audio(file.key, file.urls, file.jsonURL);
1430+
break;
1431+
14011432
case "tilemap":
14021433
this.tilemap(file.key, file.url, file.data, Phaser.Tilemap[file.format]);
14031434
break;

0 commit comments

Comments
 (0)