Skip to content

Commit 1fb96f6

Browse files
committed
Added jsdocs
1 parent 5042358 commit 1fb96f6

1 file changed

Lines changed: 101 additions & 19 deletions

File tree

src/loader/filetypes/AnimationJSONFile.js

Lines changed: 101 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,23 @@ var JSONFile = require('./JSONFile.js');
1010

1111
/**
1212
* @classdesc
13-
* [description]
13+
* A single Animation JSON File suitable for loading by the Loader.
14+
*
15+
* These are created when you use the Phaser.Loader.LoaderPlugin#animation method and are not typically created directly.
16+
*
17+
* For documentation about what all the arguments and configuration options mean please see Phaser.Loader.LoaderPlugin#animation.
1418
*
1519
* @class AnimationJSONFile
1620
* @extends Phaser.Loader.File
1721
* @memberOf Phaser.Loader.FileTypes
1822
* @constructor
1923
* @since 3.0.0
2024
*
21-
* @param {string} key - [description]
22-
* @param {string} url - [description]
23-
* @param {string} path - [description]
24-
* @param {XHRSettingsObject} [xhrSettings] - [description]
25+
* @param {Phaser.Loader.LoaderPlugin} loader - A reference to the Loader that is responsible for this file.
26+
* @param {(string|Phaser.Loader.FileTypes.JSONFileConfig)} key - The key to use for this file, or a file configuration object.
27+
* @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `<key>.json`, i.e. if `key` was "alien" then the URL will be "alien.json".
28+
* @param {XHRSettingsObject} [xhrSettings] - Extra XHR Settings specifically for this file.
29+
* @param {string} [dataKey] - When the JSON file loads only this property will be stored in the Cache.
2530
*/
2631
var AnimationJSONFile = new Class({
2732

@@ -30,14 +35,22 @@ var AnimationJSONFile = new Class({
3035
initialize:
3136

3237
// url can either be a string, in which case it is treated like a proper url, or an object, in which case it is treated as a ready-made JS Object
38+
// dataKey allows you to pluck a specific object out of the JSON and put just that into the cache, rather than the whole thing
3339

34-
function AnimationJSONFile (loader, key, url, xhrSettings)
40+
function AnimationJSONFile (loader, key, url, xhrSettings, dataKey)
3541
{
36-
JSONFile.call(this, loader, key, url, xhrSettings);
42+
JSONFile.call(this, loader, key, url, xhrSettings, dataKey);
3743

3844
this.type = 'animationJSON';
3945
},
4046

47+
/**
48+
* Called automatically by Loader.nextFile.
49+
* This method controls what extra work this File does with its loaded data.
50+
*
51+
* @method Phaser.Loader.FileTypes.AnimationJSONFile#onProcess
52+
* @since 3.7.0
53+
*/
4154
onProcess: function ()
4255
{
4356
// We need to hook into this event:
@@ -47,34 +60,103 @@ var AnimationJSONFile = new Class({
4760
JSONFile.prototype.onProcess.call(this);
4861
},
4962

63+
/**
64+
* Called at the end of the load process, after the Loader has finished all files in its queue.
65+
*
66+
* @method Phaser.Loader.FileTypes.AnimationJSONFile#onLoadComplete
67+
* @since 3.7.0
68+
*/
5069
onLoadComplete: function ()
5170
{
52-
this.loader.scene.sys.anims.fromJSON(this.data);
71+
this.loader.systems.anims.fromJSON(this.data);
5372

5473
this.pendingDestroy();
5574
}
5675

5776
});
5877

5978
/**
60-
* Adds an Animation JSON file to the current load queue.
79+
* Adds an Animation JSON Data file, or array of Animation JSON files, to the current load queue.
80+
*
81+
* The file is **not** loaded immediately, it is added to a queue ready to be loaded either when the loader starts,
82+
* or if it's already running, when the next free load slot becomes available. This means you cannot use the file
83+
* immediately after calling this method, but instead must wait for the file to complete.
84+
*
85+
* The key must be a unique String. It is used to add the file to the global JSON Cache upon a successful load.
86+
* The key should be unique both in terms of files being loaded and files already present in the JSON Cache.
87+
* Loading a file using a key that is already taken will result in a warning. If you wish to replace an existing file
88+
* then remove it from the JSON Cache first, before loading a new one.
89+
*
90+
* Instead of passing arguments you can pass a configuration object, such as:
91+
*
92+
* ```javascript
93+
* this.load.animation({
94+
* key: 'baddieAnims',
95+
* url: 'files/BaddieAnims.json'
96+
* });
97+
* ```
98+
*
99+
* See the documentation for `Phaser.Loader.FileTypes.JSONFileConfig` for more details.
100+
*
101+
* Once the file has finished loading it will automatically be passed to the global Animation Managers `fromJSON` method.
102+
* This will parse all of the JSON data and create animation data from it. This process happens at the very end
103+
* of the Loader, once every other file in the load queue has finished. The reason for this is to allow you to load
104+
* both animation data and the images it relies upon in the same load call.
105+
*
106+
* Once the animation data has been parsed you will be able to play animations using that data.
107+
* Please see the Animation Manager `fromJSON` method for more details about the format and playback.
108+
*
109+
* You can also access the raw animation data from its Cache using its key:
110+
*
111+
* ```javascript
112+
* this.load.animation('baddieAnims', 'files/BaddieAnims.json');
113+
* // and later in your game ...
114+
* var data = this.cache.json.get('baddieAnims');
115+
* ```
116+
*
117+
* If you have specified a prefix in the loader, via `Loader.setPrefix` then this value will be prepended to this files
118+
* key. For example, if the prefix was `LEVEL1.` and the key was `Waves` the final key will be `LEVEL1.Waves` and
119+
* this is what you would use to retrieve the text from the JSON Cache.
120+
*
121+
* The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it.
122+
*
123+
* If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "data"
124+
* and no URL is given then the Loader will set the URL to be "data.json". It will always add `.json` as the extension, although
125+
* this can be overridden if using an object instead of method arguments. If you do not desire this action then provide a URL.
126+
*
127+
* You can also optionally provide a `dataKey` to use. This allows you to extract only a part of the JSON and store it in the Cache,
128+
* rather than the whole file. For example, if your JSON data had a structure like this:
129+
*
130+
* ```json
131+
* {
132+
* "level1": {
133+
* "baddies": {
134+
* "aliens": {},
135+
* "boss": {}
136+
* }
137+
* },
138+
* "level2": {},
139+
* "level3": {}
140+
* }
141+
* ```
61142
*
62-
* Note: This method will only be available if the Animation JSON File type has been built into Phaser.
143+
* And if you only wanted to create animations from the `boss` data, then you could pass `level1.baddies.boss`as the `dataKey`.
63144
*
64-
* The file is **not** loaded immediately after calling this method.
65-
* Instead, the file is added to a queue within the Loader, which is processed automatically when the Loader starts.
145+
* Note: The ability to load this type of file will only be available if the JSON File type has been built into Phaser.
146+
* It is available in the default build but can be excluded from custom builds.
66147
*
67148
* @method Phaser.Loader.LoaderPlugin#animation
149+
* @fires Phaser.Loader.LoaderPlugin#addFileEvent
68150
* @since 3.0.0
69151
*
70-
* @param {(string|array|object)} key - A unique string to be used as the key to reference this file from the Cache. Must be unique within this file type.
71-
* @param {string} [url] - URL of the file. If `undefined` or `null` the url will be set to `<key>.json`,
72-
* i.e. if `key` was "alien" then the URL will be "alien.json".
73-
* @param {XHRSettingsObject} [xhrSettings] - File specific XHR settings to be used during the load. These settings are merged with the global Loader XHR settings.
152+
* @param {(string|Phaser.Loader.FileTypes.JSONFileConfig|Phaser.Loader.FileTypes.JSONFileConfig[])} key - The key to use for this file, or a file configuration object, or array of them.
153+
* @param {string} [url] - The absolute or relative URL to load this file from. If undefined or `null` it will be set to `<key>.json`, i.e. if `key` was "alien" then the URL will be "alien.json".
154+
* @param {string} [dataKey] - When the Animation JSON file loads only this property will be stored in the Cache and used to create animation data.
155+
* @param {XHRSettingsObject} [xhrSettings] - An XHR Settings configuration object. Used in replacement of the Loaders default XHR Settings.
74156
*
75-
* @return {Phaser.Loader.LoaderPlugin} The Loader.
157+
* @return {Phaser.Loader.LoaderPlugin} The Loader instance.
76158
*/
77-
FileTypesManager.register('animation', function (key, url, xhrSettings)
159+
FileTypesManager.register('animation', function (key, url, dataKey, xhrSettings)
78160
{
79161
// Supports an Object file definition in the key argument
80162
// Or an array of objects in the key argument
@@ -89,7 +171,7 @@ FileTypesManager.register('animation', function (key, url, xhrSettings)
89171
}
90172
else
91173
{
92-
this.addFile(new AnimationJSONFile(this, key, url, xhrSettings));
174+
this.addFile(new AnimationJSONFile(this, key, url, xhrSettings, dataKey));
93175
}
94176

95177
return this;

0 commit comments

Comments
 (0)