Skip to content

Commit ec50d0c

Browse files
committed
Add optional assetLoadCallback option to init
This will fire each time a file is loaded.
1 parent 7394b10 commit ec50d0c

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ View sample usage in this [Phaser ES6 Boilerplate](https://github.com/goldfire/p
1515
* Supports all filetypes.
1616
* Supports asset postfix for retina support ('@2x', '@3x', etc).
1717
* Supports automatic loading of compressed textures (PVRTC, S3TC, ETC1).
18+
* Optional callback to track each file load (including fonts).
1819

1920
## Install
2021

@@ -91,6 +92,18 @@ this.game.plugins.add(WebpackLoader, AssetManifest, '@2x')
9192
});
9293
```
9394

95+
If you want to know when each file is loaded, use the optional callback:
96+
97+
```javascript
98+
this.game.plugins.add(WebpackLoader, AssetManifest, '@2x', () => {
99+
console.log('File loaded!');
100+
})
101+
.load()
102+
.then(() => {
103+
this.game.state.start('Main');
104+
});
105+
```
106+
94107
## Loading Fonts
95108

96109
The font loader uses [Web Font Loader](https://github.com/typekit/webfontloader), which supports loading web fonts from all major providers. Simply provide their standard configuration object in your manifest.
@@ -150,6 +163,6 @@ module: {
150163

151164
### License
152165

153-
Copyright (c) 2017 [James Simpson](https://twitter.com/GoldFireStudios) and [GoldFire Studios, Inc.](http://goldfirestudios.com)
166+
Copyright (c) 2018 [James Simpson](https://twitter.com/GoldFireStudios) and [GoldFire Studios, Inc.](http://goldfirestudios.com)
154167

155168
Released under the [MIT License](https://github.com/goldfire/phaser-webpack-loader/blob/master/LICENSE.md).

src/index.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ import WebFont from 'webfontloader';
77
export default class WebpackLoader extends Phaser.Plugin {
88
/**
99
* Initialize the load plugin.
10-
* @param {Object} manifest Your asset manifest file contents.
11-
* @param {String} postfix (optional) Postfix to append to assets.
10+
* @param {Object} manifest Your asset manifest file contents.
11+
* @param {String} postfix (optional) Postfix to append to assets.
12+
* @param {Function} assetLoadCallback Callback to fire when each file has loaded.
1213
*/
13-
init(manifest, postfix = '') {
14+
init(manifest, postfix = '', assetLoadCallback) {
15+
// Define the asset load callback.
16+
this._assetLoadCallback = assetLoadCallback || (() => {});
17+
1418
// Pull the font values out of the manifest.
1519
this.fonts = manifest.fonts || {};
1620

@@ -60,8 +64,12 @@ export default class WebpackLoader extends Phaser.Plugin {
6064
});
6165
});
6266

67+
// Setup listener for each asset loading.
68+
this.game.load.onFileComplete.add(this._assetLoadCallback);
69+
6370
// Once everything has loaded, resolve the promise.
6471
this.game.load.onLoadComplete.addOnce(() => {
72+
this.game.load.onFileComplete.remove(this._assetLoadCallback);
6573
resolve();
6674
});
6775

@@ -81,7 +89,10 @@ export default class WebpackLoader extends Phaser.Plugin {
8189

8290
return new Promise((resolve) => {
8391
WebFont.load(Object.assign({}, this.fonts, {
84-
active: resolve,
92+
active: () => {
93+
this._assetLoadCallback();
94+
resolve();
95+
},
8596
}));
8697
});
8798
}

0 commit comments

Comments
 (0)