2014-03-23 16:18:31 +00:00
|
|
|
/* jshint wsh:true */
|
2013-08-28 06:02:55 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2014-02-05 05:54:25 +00:00
|
|
|
* @copyright 2014 Photon Storm Ltd.
|
2013-10-01 12:54:29 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-08-28 06:02:55 +00:00
|
|
|
* The Loader handles loading all external content such as Images, Sounds, Texture Atlases and data files.
|
|
|
|
* It uses a combination of Image() loading and xhr and provides progress and completion callbacks.
|
2014-09-23 15:21:29 +00:00
|
|
|
*
|
2013-10-01 12:54:29 +00:00
|
|
|
* @class Phaser.Loader
|
|
|
|
* @constructor
|
|
|
|
* @param {Phaser.Game} game - A reference to the currently running game.
|
2013-08-28 06:02:55 +00:00
|
|
|
*/
|
|
|
|
Phaser.Loader = function (game) {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2013-10-01 12:54:29 +00:00
|
|
|
* @property {Phaser.Game} game - Local reference to game.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
this.game = game;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} isLoading - True if the Loader is in the process of loading the queue.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.isLoading = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {boolean} hasLoaded - True if all assets in the queue have finished loading.
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.hasLoaded = false;
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2013-12-31 17:35:40 +00:00
|
|
|
* @property {number} progress - The rounded load progress percentage value (from 0 to 100)
|
2013-11-25 04:40:04 +00:00
|
|
|
* @default
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.progress = 0;
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2013-12-31 17:35:40 +00:00
|
|
|
/**
|
|
|
|
* @property {number} progressFloat - The non-rounded load progress value (from 0.0 to 100.0)
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.progressFloat = 0;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* You can optionally link a sprite to the preloader.
|
2014-02-11 13:23:54 +00:00
|
|
|
* If you do so the Sprites width or height will be cropped based on the percentage loaded.
|
2014-09-09 11:48:38 +00:00
|
|
|
* This property is an object containing: sprite, rect, direction, width and height
|
2014-09-23 15:21:29 +00:00
|
|
|
*
|
2014-09-09 11:48:38 +00:00
|
|
|
* @property {object} preloadSprite
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
this.preloadSprite = null;
|
|
|
|
|
|
|
|
/**
|
2014-04-08 02:31:13 +00:00
|
|
|
* @property {boolean|string} crossOrigin - The crossOrigin value applied to loaded images. Very often this needs to be set to 'anonymous'.
|
2014-02-11 13:23:54 +00:00
|
|
|
* @default
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-02-11 13:23:54 +00:00
|
|
|
this.crossOrigin = false;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* If you want to append a URL before the path of any asset you can set this here.
|
|
|
|
* Useful if you need to allow an asset url to be configured outside of the game code.
|
|
|
|
* MUST have / on the end of it!
|
|
|
|
* @property {string} baseURL
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this.baseURL = '';
|
|
|
|
|
2014-04-14 10:51:50 +00:00
|
|
|
/**
|
|
|
|
* @property {Phaser.Signal} onLoadStart - This event is dispatched when the loading process starts, before the first file has been requested.
|
|
|
|
*/
|
|
|
|
this.onLoadStart = new Phaser.Signal();
|
2014-04-11 18:17:26 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* @property {Phaser.Signal} onLoadComplete - This event is dispatched when the final file in the load queue has either loaded or failed.
|
2014-04-11 18:17:26 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
this.onLoadComplete = new Phaser.Signal();
|
2014-04-11 18:17:26 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* @property {Phaser.Signal} onPackComplete - This event is dispatched when an asset pack has either loaded or failed.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
this.onPackComplete = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* @property {Phaser.Signal} onFileStart - This event is dispatched immediately before a file starts loading. It's possible the file may still error (404, etc) after this event is sent.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
this.onFileStart = new Phaser.Signal();
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* @property {Phaser.Signal} onFileComplete - This event is dispatched when a file completes loading successfully.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
this.onFileComplete = new Phaser.Signal();
|
2013-09-10 19:40:34 +00:00
|
|
|
|
2014-07-13 19:38:13 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* @property {Phaser.Signal} onFileError - This event is dispatched when a file errors as a result of the load request.
|
2014-07-13 19:38:13 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
this.onFileError = new Phaser.Signal();
|
2014-09-23 15:21:29 +00:00
|
|
|
|
2014-08-28 00:23:45 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* @property {boolean} useXDomainRequest - If true and if the browser supports XDomainRequest, it will be used in preference for XHR when loading JSON files (it does not affect other file types). This is only relevant for IE9 and should only be enabled when you know your server/CDN requires it.
|
2014-08-28 00:23:45 +00:00
|
|
|
*/
|
2014-10-27 12:31:55 +00:00
|
|
|
this.useXDomainRequest = false;
|
2014-08-28 00:23:45 +00:00
|
|
|
|
2014-05-29 14:57:01 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* Contains all queued pack information to load.
|
|
|
|
* Packs are removed from this list as they are processed.
|
|
|
|
*
|
2014-05-29 14:57:01 +00:00
|
|
|
* @property {array} _packList - Contains all the assets packs.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._packList = [];
|
|
|
|
|
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* Contains all the information for asset files that need to be loaded.
|
|
|
|
* Files are removed from this list as they are processed.
|
|
|
|
*
|
|
|
|
* @property {array} _fileList
|
2014-05-29 14:57:01 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._fileList = [];
|
|
|
|
|
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* The total number of files/assets to be loaded.
|
|
|
|
* This may increase after processing packs.
|
|
|
|
*
|
|
|
|
* @property {number} _totalFileCount
|
2014-05-29 14:57:01 +00:00
|
|
|
* @private
|
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
this._totalFileCount = 0;
|
|
|
|
|
|
|
|
this._loadedFileCount = 0;
|
|
|
|
this._failedFileCount = 0;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @property {number} _progressChunk - Indicates the size of 1 file in terms of a percentage out of 100.
|
|
|
|
* @private
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
this._progressChunk = 0;
|
|
|
|
|
2013-09-10 19:40:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2013-10-02 14:05:55 +00:00
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY = 0;
|
2013-10-02 14:05:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
Phaser.Loader.TEXTURE_ATLAS_JSON_HASH = 1;
|
2013-10-02 14:05:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2013-09-10 19:40:34 +00:00
|
|
|
Phaser.Loader.TEXTURE_ATLAS_XML_STARLING = 2;
|
|
|
|
|
2014-02-14 23:51:49 +00:00
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
2014-03-31 07:46:17 +00:00
|
|
|
Phaser.Loader.PHYSICS_LIME_CORONA_JSON = 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {number}
|
|
|
|
*/
|
|
|
|
Phaser.Loader.PHYSICS_PHASER_JSON = 4;
|
2014-02-14 23:51:49 +00:00
|
|
|
|
2013-09-10 19:40:34 +00:00
|
|
|
Phaser.Loader.prototype = {
|
2013-10-02 14:05:55 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* You can set a Sprite to be a "preload" sprite by passing it to this method.
|
|
|
|
* A "preload" sprite will have its width or height crop adjusted based on the percentage of the loader in real-time.
|
2014-02-21 17:29:51 +00:00
|
|
|
* This allows you to easily make loading bars for games. Note that Sprite.visible = true will be set when calling this.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader#setPreloadSprite
|
2014-03-18 16:23:44 +00:00
|
|
|
* @param {Phaser.Sprite|Phaser.Image} sprite - The sprite or image that will be cropped during the load.
|
|
|
|
* @param {number} [direction=0] - A value of zero means the sprite will be cropped horizontally, a value of 1 means its will be cropped vertically.
|
2013-10-01 12:54:29 +00:00
|
|
|
*/
|
2013-11-25 04:40:04 +00:00
|
|
|
setPreloadSprite: function (sprite, direction) {
|
|
|
|
|
|
|
|
direction = direction || 0;
|
|
|
|
|
2014-03-18 16:23:44 +00:00
|
|
|
this.preloadSprite = { sprite: sprite, direction: direction, width: sprite.width, height: sprite.height, rect: null };
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
if (direction === 0)
|
|
|
|
{
|
2014-03-18 16:23:44 +00:00
|
|
|
// Horizontal rect
|
|
|
|
this.preloadSprite.rect = new Phaser.Rectangle(0, 0, 1, sprite.height);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-18 16:23:44 +00:00
|
|
|
// Vertical rect
|
|
|
|
this.preloadSprite.rect = new Phaser.Rectangle(0, 0, sprite.width, 1);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-18 16:23:44 +00:00
|
|
|
sprite.crop(this.preloadSprite.rect);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-02-21 17:29:51 +00:00
|
|
|
sprite.visible = true;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
|
|
|
|
2014-11-11 14:00:07 +00:00
|
|
|
/**
|
|
|
|
* Called automatically by ScaleManager when the game resizes in RESIZE scalemode.
|
|
|
|
* We use this to adjust the height of the preloading sprite, if set.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#resize
|
|
|
|
*/
|
|
|
|
resize: function () {
|
|
|
|
|
|
|
|
if (this.preloadSprite && this.preloadSprite.height !== this.preloadSprite.sprite.height)
|
|
|
|
{
|
|
|
|
this.preloadSprite.rect.height = this.preloadSprite.sprite.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Check whether asset exists with a specific key.
|
2014-05-08 00:15:02 +00:00
|
|
|
* Use Phaser.Cache to access loaded assets, e.g. Phaser.Cache#checkImageKey
|
2014-09-23 15:21:29 +00:00
|
|
|
*
|
2013-11-25 04:40:04 +00:00
|
|
|
* @method Phaser.Loader#checkKeyExists
|
2013-11-26 15:29:03 +00:00
|
|
|
* @param {string} type - The type asset you want to check.
|
2013-11-25 04:40:04 +00:00
|
|
|
* @param {string} key - Key of the asset you want to check.
|
|
|
|
* @return {boolean} Return true if exists, otherwise return false.
|
|
|
|
*/
|
2013-11-26 15:29:03 +00:00
|
|
|
checkKeyExists: function (type, key) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
return this.getAssetIndex(type, key) > -1;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
},
|
|
|
|
|
2014-02-27 21:41:54 +00:00
|
|
|
/**
|
|
|
|
* Gets the fileList index for the given key.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#getAssetIndex
|
|
|
|
* @param {string} type - The type asset you want to check.
|
|
|
|
* @param {string} key - Key of the asset you want to check.
|
|
|
|
* @return {number} The index of this key in the filelist, or -1 if not found.
|
|
|
|
*/
|
|
|
|
getAssetIndex: function (type, key) {
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
for (var i = 0; i < this._fileList.length; i++)
|
2014-02-27 21:41:54 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
if (this._fileList[i].type === type && this._fileList[i].key === key)
|
2014-02-27 21:41:54 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
return i;
|
2014-02-27 21:41:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2014-02-27 21:41:54 +00:00
|
|
|
},
|
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
/**
|
|
|
|
* Gets the asset that is queued for load.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#getAsset
|
|
|
|
* @param {string} type - The type asset you want to check.
|
|
|
|
* @param {string} key - Key of the asset you want to check.
|
2014-11-21 01:51:30 +00:00
|
|
|
* @return {any} Returns an object if found that has 2 properties: index and asset entry. Otherwise false.
|
2013-11-26 15:29:03 +00:00
|
|
|
*/
|
|
|
|
getAsset: function (type, key) {
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
var fileIndex = this.getAssetIndex(type, key);
|
|
|
|
|
|
|
|
if (fileIndex > -1)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
return { index: fileIndex, file: this._fileList[fileIndex] };
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-11-26 15:29:03 +00:00
|
|
|
|
|
|
|
return false;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-26 15:29:03 +00:00
|
|
|
* Reset loader, this will remove the load queue.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader#reset
|
|
|
|
*/
|
|
|
|
reset: function () {
|
|
|
|
|
|
|
|
this.preloadSprite = null;
|
|
|
|
this.isLoading = false;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
|
|
|
this._packList.length = 0;
|
2013-11-26 15:29:03 +00:00
|
|
|
this._fileList.length = 0;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function that adds a new entry to the file list. Do not call directly.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#addToFileList
|
2014-05-29 14:57:01 +00:00
|
|
|
* @protected
|
2013-11-26 15:29:03 +00:00
|
|
|
* @param {string} type - The type of resource to add to the list (image, audio, xml, etc).
|
|
|
|
* @param {string} key - The unique Cache ID key of this resource.
|
|
|
|
* @param {string} url - The URL the asset will be loaded from.
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {object} [properties] - Any additional properties needed to load the file.
|
|
|
|
* @param {boolean} [overwrite=false] - If true then this will overwrite an aspect of the same type/key. Otherwise it will will only add a new asset.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
addToFileList: function (type, key, url, properties, overwrite) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
var entry = {
|
|
|
|
type: type,
|
|
|
|
key: key,
|
|
|
|
url: url,
|
|
|
|
data: null,
|
|
|
|
error: false,
|
|
|
|
loaded: false
|
|
|
|
};
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (properties)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
|
|
|
for (var prop in properties)
|
|
|
|
{
|
|
|
|
entry[prop] = properties[prop];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
var fileIndex = this.getAssetIndex(type, key);
|
|
|
|
|
|
|
|
if (overwrite && fileIndex > -1)
|
|
|
|
{
|
|
|
|
// Could potentially overwrite an already loaded asset..
|
|
|
|
this._fileList[fileIndex] = entry;
|
|
|
|
}
|
|
|
|
else if (assetIndex === -1)
|
2013-11-26 15:29:03 +00:00
|
|
|
{
|
|
|
|
this._fileList.push(entry);
|
|
|
|
}
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function that replaces an existing entry in the file list with a new one. Do not call directly.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#replaceInFileList
|
|
|
|
* @param {string} type - The type of resource to add to the list (image, audio, xml, etc).
|
|
|
|
* @param {string} key - The unique Cache ID key of this resource.
|
|
|
|
* @param {string} url - The URL the asset will be loaded from.
|
|
|
|
* @param {object} properties - Any additional properties needed to load the file.
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
replaceInFileList: function (type, key, url, properties) {
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
return addToFileList(type, key, url, properties, true);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-05-29 14:57:01 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* Add a JSON resource pack to the Loader.
|
2014-05-29 14:57:01 +00:00
|
|
|
*
|
2014-11-21 01:51:30 +00:00
|
|
|
* @method Phaser.Loader#pack
|
|
|
|
* @param {string} key - Unique asset key of this resource pack.
|
2014-05-29 14:57:01 +00:00
|
|
|
* @param {string} [url] - URL of the Asset Pack JSON file. If you wish to pass a json object instead set this to null and pass the object as the data parameter.
|
|
|
|
* @param {object} [data] - The Asset Pack JSON data. Use this to pass in a json data object rather than loading it from a URL. TODO
|
|
|
|
* @param {object} [callbackContext] - Some Loader operations, like Binary and Script require a context for their callbacks. Pass the context here.
|
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
|
|
|
*/
|
|
|
|
pack: function (key, url, data, callbackContext) {
|
|
|
|
|
|
|
|
if (typeof url === "undefined") { url = null; }
|
|
|
|
if (typeof data === "undefined") { data = null; }
|
|
|
|
if (typeof callbackContext === "undefined") { callbackContext = this; }
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (!url && !data)
|
2014-05-29 14:57:01 +00:00
|
|
|
{
|
|
|
|
console.warn('Phaser.Loader.pack - Both url and data are null. One must be set.');
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A data object has been given
|
|
|
|
if (data)
|
|
|
|
{
|
|
|
|
if (typeof data === 'string')
|
|
|
|
{
|
|
|
|
data = JSON.parse(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this._packList.push( { key: key, url: url, data: data, loaded: false, error: false, callbackContext: callbackContext } );
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Add an image to the Loader.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#image
|
|
|
|
* @param {string} key - Unique asset key of this image file.
|
|
|
|
* @param {string} url - URL of image file.
|
2013-11-26 15:29:03 +00:00
|
|
|
* @param {boolean} [overwrite=false] - If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
image: function (key, url, overwrite) {
|
|
|
|
|
|
|
|
if (typeof overwrite === "undefined") { overwrite = false; }
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.addToFileList('image', key, url, undefined, overwrite);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a text file to the Loader.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#text
|
|
|
|
* @param {string} key - Unique asset key of the text file.
|
|
|
|
* @param {string} url - URL of the text file.
|
2013-11-26 15:29:03 +00:00
|
|
|
* @param {boolean} [overwrite=false] - If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
text: function (key, url, overwrite) {
|
|
|
|
|
|
|
|
if (typeof overwrite === "undefined") { overwrite = false; }
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.addToFileList('text', key, url, undefined, overwrite);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-21 18:48:06 +00:00
|
|
|
/**
|
|
|
|
* Add a json file to the Loader.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#json
|
|
|
|
* @param {string} key - Unique asset key of the json file.
|
|
|
|
* @param {string} url - URL of the json file.
|
|
|
|
* @param {boolean} [overwrite=false] - If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.
|
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
|
|
|
*/
|
|
|
|
json: function (key, url, overwrite) {
|
|
|
|
|
|
|
|
if (typeof overwrite === "undefined") { overwrite = false; }
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.addToFileList('json', key, url, undefined, overwrite);
|
2014-02-21 18:48:06 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-09-18 23:22:00 +00:00
|
|
|
/**
|
|
|
|
* Add an XML file to the Loader.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#xml
|
|
|
|
* @param {string} key - Unique asset key of the xml file.
|
|
|
|
* @param {string} url - URL of the xml file.
|
|
|
|
* @param {boolean} [overwrite=false] - If an unloaded file with a matching key already exists in the queue, this entry will overwrite it.
|
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
|
|
|
*/
|
|
|
|
xml: function (key, url, overwrite) {
|
|
|
|
|
|
|
|
if (typeof overwrite === "undefined") { overwrite = false; }
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.addToFileList('xml', key, url, undefined, overwrite);
|
2014-09-18 23:22:00 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-28 05:43:35 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* Add a JavaScript file to the Loader.
|
|
|
|
*
|
|
|
|
* The loaded JavaScript is automatically turned into a script tag and executed, so be careful what you load!
|
|
|
|
*
|
|
|
|
* A callback, which will be invoked as the script tag has been created, can also be specified.
|
|
|
|
* The callback must return relevant `data`.
|
2013-11-28 05:43:35 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader#script
|
|
|
|
* @param {string} key - Unique asset key of the script file.
|
|
|
|
* @param {string} url - URL of the JavaScript file.
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {function} [callback=(none)] - Optional callback that will be called after the script tag has loaded, so you can perform additional processing.
|
|
|
|
* @param {object} [callbackContext=(Loader)] - The context under which the callback will be applied. If not specified it will use the callback itself as the context.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-28 05:43:35 +00:00
|
|
|
*/
|
2014-02-26 02:45:06 +00:00
|
|
|
script: function (key, url, callback, callbackContext) {
|
|
|
|
|
|
|
|
if (typeof callback === 'undefined') { callback = false; }
|
2014-11-21 01:51:30 +00:00
|
|
|
// PST-FIXME-WARN Why is the default callback context the ..callback?
|
2014-02-26 02:45:06 +00:00
|
|
|
if (callback !== false && typeof callbackContext === 'undefined') { callbackContext = callback; }
|
2013-11-28 05:43:35 +00:00
|
|
|
|
2014-02-26 02:45:06 +00:00
|
|
|
this.addToFileList('script', key, url, { callback: callback, callbackContext: callbackContext });
|
2013-11-28 05:43:35 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-12-20 18:27:36 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* Add a binary file to the Loader.
|
|
|
|
*
|
|
|
|
* It will be loaded via xhr with a responseType of "arraybuffer". You can specify an optional callback to process the file after load.
|
2013-12-20 18:27:36 +00:00
|
|
|
* When the callback is called it will be passed 2 parameters: the key of the file and the file data.
|
2014-11-21 01:51:30 +00:00
|
|
|
*
|
|
|
|
* WARNING: If a callback is specified the data will be set to whatever it returns. Always return the data object, even if you didn't modify it.
|
2013-12-20 18:27:36 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader#binary
|
|
|
|
* @param {string} key - Unique asset key of the binary file.
|
|
|
|
* @param {string} url - URL of the binary file.
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {function} [callback=(none)] - Optional callback that will be passed the file after loading, so you can perform additional processing on it.
|
|
|
|
* @param {object} [callbackContext] - The context under which the callback will be applied. If not specified it will use the callback itself as the context.
|
2013-12-20 18:27:36 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
|
|
|
*/
|
|
|
|
binary: function (key, url, callback, callbackContext) {
|
|
|
|
|
|
|
|
if (typeof callback === 'undefined') { callback = false; }
|
|
|
|
if (callback !== false && typeof callbackContext === 'undefined') { callbackContext = callback; }
|
|
|
|
|
|
|
|
this.addToFileList('binary', key, url, { callback: callback, callbackContext: callbackContext });
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Add a new sprite sheet to the loader.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#spritesheet
|
|
|
|
* @param {string} key - Unique asset key of the sheet file.
|
|
|
|
* @param {string} url - URL of the sheet file.
|
|
|
|
* @param {number} frameWidth - Width of each single frame.
|
|
|
|
* @param {number} frameHeight - Height of each single frame.
|
|
|
|
* @param {number} [frameMax=-1] - How many frames in this sprite sheet. If not specified it will divide the whole image into frames.
|
2013-12-13 14:04:14 +00:00
|
|
|
* @param {number} [margin=0] - If the frames have been drawn with a margin, specify the amount here.
|
|
|
|
* @param {number} [spacing=0] - If the frames have been drawn with spacing between them, specify the amount here.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-12-13 14:04:14 +00:00
|
|
|
spritesheet: function (key, url, frameWidth, frameHeight, frameMax, margin, spacing) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
if (typeof frameMax === "undefined") { frameMax = -1; }
|
2013-12-13 14:04:14 +00:00
|
|
|
if (typeof margin === "undefined") { margin = 0; }
|
|
|
|
if (typeof spacing === "undefined") { spacing = 0; }
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-12-13 14:04:14 +00:00
|
|
|
this.addToFileList('spritesheet', key, url, { frameWidth: frameWidth, frameHeight: frameHeight, frameMax: frameMax, margin: margin, spacing: spacing });
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new audio file to the loader.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#audio
|
|
|
|
* @param {string} key - Unique asset key of the audio file.
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {string[]|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.
|
|
|
|
* @param {boolean} [autoDecode=true] - 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.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
audio: function (key, urls, autoDecode) {
|
|
|
|
|
|
|
|
if (typeof autoDecode === "undefined") { autoDecode = true; }
|
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
this.addToFileList('audio', key, urls, { buffer: null, autoDecode: autoDecode });
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-09-23 15:21:29 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* Add a new audiosprite file to the loader.
|
|
|
|
*
|
|
|
|
* Audio Sprites are a combination of audio files and a JSON configuration.
|
2014-09-23 21:15:09 +00:00
|
|
|
* The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite
|
2014-09-23 15:25:49 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader#audiosprite
|
|
|
|
* @param {string} key - Unique asset key of the audio file.
|
|
|
|
* @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.
|
2014-09-23 21:15:09 +00:00
|
|
|
* @param {string} atlasURL - The URL of the audiosprite configuration json.
|
2014-09-23 15:25:49 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
|
|
|
*/
|
2014-09-23 21:15:09 +00:00
|
|
|
audiosprite: function(key, urls, atlasURL) {
|
|
|
|
|
2014-09-23 15:25:49 +00:00
|
|
|
this.audio(key, urls);
|
2014-09-23 21:15:09 +00:00
|
|
|
|
|
|
|
this.json(key + '-audioatlas', atlasURL);
|
|
|
|
|
2014-09-23 15:21:29 +00:00
|
|
|
return this;
|
2014-09-23 21:15:09 +00:00
|
|
|
|
2014-09-23 15:21:29 +00:00
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Add a new tilemap loading request.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#tilemap
|
|
|
|
* @param {string} key - Unique asset key of the tilemap data.
|
2014-05-29 03:44:23 +00:00
|
|
|
* @param {string} [url] - The url of the map data file (csv/json)
|
|
|
|
* @param {object} [data] - An optional JSON data object. If given then the url is ignored and this JSON object is used for map data instead.
|
2014-03-06 16:53:52 +00:00
|
|
|
* @param {number} [format=Phaser.Tilemap.CSV] - The format of the map data. Either Phaser.Tilemap.CSV or Phaser.Tilemap.TILED_JSON.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-05-29 03:44:23 +00:00
|
|
|
tilemap: function (key, url, data, format) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-05-29 03:44:23 +00:00
|
|
|
if (typeof url === "undefined") { url = null; }
|
|
|
|
if (typeof data === "undefined") { data = null; }
|
2013-11-25 04:40:04 +00:00
|
|
|
if (typeof format === "undefined") { format = Phaser.Tilemap.CSV; }
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (!url && !data)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2014-05-29 03:44:23 +00:00
|
|
|
console.warn('Phaser.Loader.tilemap - Both url and data are null. One must be set.');
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2013-11-28 14:22:47 +00:00
|
|
|
// A map data object has been given
|
2014-05-29 03:44:23 +00:00
|
|
|
if (data)
|
2013-11-26 15:29:03 +00:00
|
|
|
{
|
|
|
|
switch (format)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2013-11-26 15:29:03 +00:00
|
|
|
// A csv string or object has been given
|
|
|
|
case Phaser.Tilemap.CSV:
|
|
|
|
break;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
// An xml string or object has been given
|
|
|
|
case Phaser.Tilemap.TILED_JSON:
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-05-29 03:44:23 +00:00
|
|
|
if (typeof data === 'string')
|
2013-11-26 15:29:03 +00:00
|
|
|
{
|
2014-05-29 03:44:23 +00:00
|
|
|
data = JSON.parse(data);
|
2013-11-26 15:29:03 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-11-26 15:29:03 +00:00
|
|
|
|
2014-05-29 03:44:23 +00:00
|
|
|
this.game.cache.addTilemap(key, null, data, format);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-11-28 14:22:47 +00:00
|
|
|
else
|
|
|
|
{
|
2014-05-29 03:44:23 +00:00
|
|
|
this.addToFileList('tilemap', key, url, { format: format });
|
2013-11-28 14:22:47 +00:00
|
|
|
}
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-02-14 23:51:49 +00:00
|
|
|
/**
|
|
|
|
* Add a new physics data object loading request.
|
2014-11-21 01:51:30 +00:00
|
|
|
*
|
2014-02-14 23:51:49 +00:00
|
|
|
* The data must be in Lime + Corona JSON format. Physics Editor by code'n'web exports in this format natively.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#physics
|
|
|
|
* @param {string} key - Unique asset key of the physics json data.
|
2014-05-29 03:44:23 +00:00
|
|
|
* @param {string} [url] - The url of the map data file (csv/json)
|
|
|
|
* @param {object} [data] - An optional JSON data object. If given then the url is ignored and this JSON object is used for physics data instead.
|
2014-02-14 23:51:49 +00:00
|
|
|
* @param {string} [format=Phaser.Physics.LIME_CORONA_JSON] - The format of the physics data.
|
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
|
|
|
*/
|
2014-05-29 03:44:23 +00:00
|
|
|
physics: function (key, url, data, format) {
|
2014-02-14 23:51:49 +00:00
|
|
|
|
2014-05-29 03:44:23 +00:00
|
|
|
if (typeof url === "undefined") { url = null; }
|
|
|
|
if (typeof data === "undefined") { data = null; }
|
2014-03-31 08:19:08 +00:00
|
|
|
if (typeof format === "undefined") { format = Phaser.Physics.LIME_CORONA_JSON; }
|
2014-02-14 23:51:49 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (!url && !data)
|
2014-02-14 23:51:49 +00:00
|
|
|
{
|
2014-05-29 03:44:23 +00:00
|
|
|
console.warn('Phaser.Loader.physics - Both url and data are null. One must be set.');
|
2014-02-14 23:51:49 +00:00
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A map data object has been given
|
2014-05-29 03:44:23 +00:00
|
|
|
if (data)
|
2014-02-14 23:51:49 +00:00
|
|
|
{
|
2014-05-29 03:44:23 +00:00
|
|
|
if (typeof data === 'string')
|
2014-02-14 23:51:49 +00:00
|
|
|
{
|
2014-05-29 03:44:23 +00:00
|
|
|
data = JSON.parse(data);
|
2014-02-14 23:51:49 +00:00
|
|
|
}
|
|
|
|
|
2014-05-29 03:44:23 +00:00
|
|
|
this.game.cache.addPhysicsData(key, null, data, format);
|
2014-02-14 23:51:49 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-29 03:44:23 +00:00
|
|
|
this.addToFileList('physics', key, url, { format: format });
|
2014-02-14 23:51:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Add a new bitmap font loading request.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#bitmapFont
|
|
|
|
* @param {string} key - Unique asset key of the bitmap font.
|
|
|
|
* @param {string} textureURL - The url of the font image file.
|
|
|
|
* @param {string} [xmlURL] - The url of the font data file (xml/fnt)
|
|
|
|
* @param {object} [xmlData] - An optional XML data object.
|
2014-02-14 04:34:57 +00:00
|
|
|
* @param {number} [xSpacing=0] - If you'd like to add additional horizontal spacing between the characters then set the pixel value here.
|
|
|
|
* @param {number} [ySpacing=0] - If you'd like to add additional vertical spacing between the lines then set the pixel value here.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-02-14 04:34:57 +00:00
|
|
|
bitmapFont: function (key, textureURL, xmlURL, xmlData, xSpacing, ySpacing) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
if (typeof xmlURL === "undefined") { xmlURL = null; }
|
|
|
|
if (typeof xmlData === "undefined") { xmlData = null; }
|
2014-02-14 04:34:57 +00:00
|
|
|
if (typeof xSpacing === "undefined") { xSpacing = 0; }
|
|
|
|
if (typeof ySpacing === "undefined") { ySpacing = 0; }
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
// A URL to a json/xml file has been given
|
|
|
|
if (xmlURL)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2014-02-14 04:34:57 +00:00
|
|
|
this.addToFileList('bitmapfont', key, textureURL, { xmlURL: xmlURL, xSpacing: xSpacing, ySpacing: ySpacing });
|
2013-11-26 15:29:03 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// An xml string or object has been given
|
|
|
|
if (typeof xmlData === 'string')
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
var xml = this.parseXml(xmlData);
|
2013-11-26 15:29:03 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (!xml)
|
2013-11-26 15:29:03 +00:00
|
|
|
{
|
|
|
|
throw new Error("Phaser.Loader. Invalid Bitmap Font XML given");
|
|
|
|
}
|
2014-11-21 01:51:30 +00:00
|
|
|
|
|
|
|
this.addToFileList('bitmapfont', key, textureURL, { xmlURL: null, xmlData: xml, xSpacing: xSpacing, ySpacing: ySpacing });
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new texture atlas to the loader. This atlas uses the JSON Array data format.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#atlasJSONArray
|
2013-11-26 15:29:03 +00:00
|
|
|
* @param {string} key - Unique asset key of the texture atlas file.
|
|
|
|
* @param {string} textureURL - The url of the texture atlas image file.
|
|
|
|
* @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.
|
|
|
|
* @param {object} [atlasData] - A JSON or XML data object. You don't need this if the data is being loaded from a URL.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
atlasJSONArray: function (key, textureURL, atlasURL, atlasData) {
|
|
|
|
|
|
|
|
return this.atlas(key, textureURL, atlasURL, atlasData, Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new texture atlas to the loader. This atlas uses the JSON Hash data format.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#atlasJSONHash
|
2013-11-26 15:29:03 +00:00
|
|
|
* @param {string} key - Unique asset key of the texture atlas file.
|
|
|
|
* @param {string} textureURL - The url of the texture atlas image file.
|
|
|
|
* @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.
|
|
|
|
* @param {object} [atlasData] - A JSON or XML data object. You don't need this if the data is being loaded from a URL.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
atlasJSONHash: function (key, textureURL, atlasURL, atlasData) {
|
|
|
|
|
|
|
|
return this.atlas(key, textureURL, atlasURL, atlasData, Phaser.Loader.TEXTURE_ATLAS_JSON_HASH);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new texture atlas to the loader. This atlas uses the Starling XML data format.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#atlasXML
|
2013-11-26 15:29:03 +00:00
|
|
|
* @param {string} key - Unique asset key of the texture atlas file.
|
|
|
|
* @param {string} textureURL - The url of the texture atlas image file.
|
|
|
|
* @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.
|
|
|
|
* @param {object} [atlasData] - A JSON or XML data object. You don't need this if the data is being loaded from a URL.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
atlasXML: function (key, textureURL, atlasURL, atlasData) {
|
|
|
|
|
|
|
|
return this.atlas(key, textureURL, atlasURL, atlasData, Phaser.Loader.TEXTURE_ATLAS_XML_STARLING);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a new texture atlas to the loader.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#atlas
|
|
|
|
* @param {string} key - Unique asset key of the texture atlas file.
|
|
|
|
* @param {string} textureURL - The url of the texture atlas image file.
|
|
|
|
* @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.
|
|
|
|
* @param {object} [atlasData] - A JSON or XML data object. You don't need this if the data is being loaded from a URL.
|
|
|
|
* @param {number} [format] - A value describing the format of the data, the default is Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY.
|
2013-11-28 14:22:47 +00:00
|
|
|
* @return {Phaser.Loader} This Loader instance.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
atlas: function (key, textureURL, atlasURL, atlasData, format) {
|
|
|
|
|
|
|
|
if (typeof atlasURL === "undefined") { atlasURL = null; }
|
|
|
|
if (typeof atlasData === "undefined") { atlasData = null; }
|
|
|
|
if (typeof format === "undefined") { format = Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY; }
|
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
// A URL to a json/xml file has been given
|
|
|
|
if (atlasURL)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2013-11-26 15:29:03 +00:00
|
|
|
this.addToFileList('textureatlas', key, textureURL, { atlasURL: atlasURL, format: format });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (format)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2013-11-26 15:29:03 +00:00
|
|
|
// A json string or object has been given
|
|
|
|
case Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY:
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
if (typeof atlasData === 'string')
|
|
|
|
{
|
|
|
|
atlasData = JSON.parse(atlasData);
|
|
|
|
}
|
|
|
|
break;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
// An xml string or object has been given
|
|
|
|
case Phaser.Loader.TEXTURE_ATLAS_XML_STARLING:
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
if (typeof atlasData === 'string')
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
var xml = this.parseXml(atlasData);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (!xml)
|
2013-11-26 15:29:03 +00:00
|
|
|
{
|
|
|
|
throw new Error("Phaser.Loader. Invalid Texture Atlas XML given");
|
|
|
|
}
|
2014-11-21 01:51:30 +00:00
|
|
|
|
|
|
|
atlasData = xml;
|
2013-11-26 15:29:03 +00:00
|
|
|
}
|
|
|
|
break;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
this.addToFileList('textureatlas', key, textureURL, { atlasURL: null, atlasData: atlasData, format: format });
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove loading request of a file.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#removeFile
|
2014-11-21 01:51:30 +00:00
|
|
|
* @protected
|
2013-11-26 15:29:03 +00:00
|
|
|
* @param {string} type - The type of resource to add to the list (image, audio, xml, etc).
|
|
|
|
* @param {string} key - Key of the file you want to remove.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2013-11-26 15:29:03 +00:00
|
|
|
removeFile: function (type, key) {
|
|
|
|
|
|
|
|
var file = this.getAsset(type, key);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (file)
|
2013-11-26 15:29:03 +00:00
|
|
|
{
|
|
|
|
this._fileList.splice(file.index, 1);
|
|
|
|
}
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all file loading requests.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#removeAll
|
|
|
|
*/
|
|
|
|
removeAll: function () {
|
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
this._fileList.length = 0;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Start loading the assets. Normally you don't need to call this yourself as the StateManager will do so.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#start
|
|
|
|
*/
|
|
|
|
start: function () {
|
|
|
|
|
|
|
|
if (this.isLoading)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-05-29 14:57:01 +00:00
|
|
|
if (this._packList.length > 0)
|
|
|
|
{
|
|
|
|
this.loadPack();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.beginLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
/**
|
|
|
|
* Process the next item in the file/asset queue.
|
|
|
|
* - fileList: pickup queue
|
|
|
|
* - inflight: in flight requests
|
|
|
|
* - inflight is removed on error/success
|
|
|
|
* - inflight is only processed in order for certain types
|
|
|
|
* - inflight is always sequential by queue order
|
|
|
|
* - loaded, loading, error - all cycles back to queue
|
|
|
|
* - loadStartedAt, finishedAt (by process)
|
|
|
|
* - fetched (item, data) -> handler -> loaded -> process
|
|
|
|
* -
|
|
|
|
* - packs must be processed in order, but can be downloaded parallel
|
|
|
|
* - packs must be processed first
|
|
|
|
* - a pack must be immediately added to the processing/inflight
|
|
|
|
*/
|
|
|
|
pump: function (item, success) {
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-05-29 14:57:01 +00:00
|
|
|
/**
|
|
|
|
* Starts off the actual loading process after the asset packs have been sorted out.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#beginLoad
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
beginLoad: function () {
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.progress = 0;
|
2013-12-31 17:35:40 +00:00
|
|
|
this.progressFloat = 0;
|
2013-11-25 04:40:04 +00:00
|
|
|
this.hasLoaded = false;
|
|
|
|
this.isLoading = true;
|
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
this.onLoadStart.dispatch(this._fileList.length);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (this._fileList.length)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
this._progressChunk = 100 / this._totalFileCount;
|
2013-11-25 04:40:04 +00:00
|
|
|
this.loadFile();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.progress = 100;
|
2013-12-31 17:35:40 +00:00
|
|
|
this.progressFloat = 100;
|
2013-11-25 04:40:04 +00:00
|
|
|
this.hasLoaded = true;
|
2014-06-30 08:24:25 +00:00
|
|
|
this.isLoading = false;
|
2013-11-25 04:40:04 +00:00
|
|
|
this.onLoadComplete.dispatch();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-05-29 14:57:01 +00:00
|
|
|
/**
|
|
|
|
* Loads the current asset pack in the queue.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#loadPack
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
loadPack: function () {
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
var pack = this._packList.shift();
|
|
|
|
|
|
|
|
if (!pack)
|
2014-05-29 14:57:01 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
console.warn('Phaser.Loader loadPack: no more packs');
|
2014-05-29 14:57:01 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-23 15:21:29 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (pack.data)
|
2014-05-29 14:57:01 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
var packData = pack.data[pack.key];
|
|
|
|
if (packData)
|
|
|
|
{
|
|
|
|
this.processPackData(packData);
|
|
|
|
}
|
|
|
|
this.nextPack(pack, true;)
|
2014-05-29 14:57:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Load it
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(pack, this.baseURL + pack.url, 'text', 'packLoadComplete', 'packLoadError');
|
2014-05-29 14:57:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the successful loading of a JSON asset pack.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#packLoadComplete
|
|
|
|
* @private
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {object} pack - Pack associated with this request
|
|
|
|
* @param {XMLHttpRequest} xhr
|
2014-05-29 14:57:01 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
packLoadComplete: function (pack, xhr) {
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
pack.loaded = true;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
var data = JSON.parse(xhr.responseText);
|
|
|
|
var packData = data[pack.key];
|
|
|
|
if (packData)
|
2014-05-29 14:57:01 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
processPackData(packData);
|
2014-05-29 14:57:01 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.nextPack(pack, true);
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
},
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
processPackData: function (packData) {
|
|
|
|
var file;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
for (var i = 0; i < packData.length; i++)
|
2014-05-29 14:57:01 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
file = packData[i];
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
switch (file.type)
|
2014-05-29 14:57:01 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
case "image":
|
|
|
|
this.image(file.key, file.url, file.overwrite);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "text":
|
|
|
|
this.text(file.key, file.url, file.overwrite);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "json":
|
|
|
|
this.json(file.key, file.url, file.overwrite);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "xml":
|
|
|
|
this.xml(file.key, file.url, file.overwrite);
|
|
|
|
break;
|
2014-09-18 23:22:00 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "script":
|
|
|
|
this.script(file.key, file.url, file.callback, pack.callbackContext);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "binary":
|
|
|
|
this.binary(file.key, file.url, file.callback, pack.callbackContext);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "spritesheet":
|
|
|
|
this.spritesheet(file.key, file.url, file.frameWidth, file.frameHeight, file.frameMax, file.margin, file.spacing);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "audio":
|
|
|
|
this.audio(file.key, file.urls, file.autoDecode);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "tilemap":
|
|
|
|
this.tilemap(file.key, file.url, file.data, Phaser.Tilemap[file.format]);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "physics":
|
|
|
|
this.physics(file.key, file.url, file.data, Phaser.Loader[file.format]);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "bitmapFont":
|
|
|
|
this.bitmapFont(file.key, file.textureURL, file.xmlURL, file.xmlData, file.xSpacing, file.ySpacing);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "atlasJSONArray":
|
|
|
|
this.atlasJSONArray(file.key, file.textureURL, file.atlasURL, file.atlasData);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "atlasJSONHash":
|
|
|
|
this.atlasJSONHash(file.key, file.textureURL, file.atlasURL, file.atlasData);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "atlasXML":
|
|
|
|
this.atlasXML(file.key, file.textureURL, file.atlasURL, file.atlasData);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
case "atlas":
|
|
|
|
this.atlas(file.key, file.textureURL, file.atlasURL, file.atlasData, Phaser.Loader[file.format]);
|
|
|
|
break;
|
2014-05-29 14:57:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error occured when loading an asset pack.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#packError
|
|
|
|
* @private
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {object} pack - Pack associated with this request
|
|
|
|
* @param {?XMLHttpRequest} xhr
|
2014-05-29 14:57:01 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
packError: function (pack, xhr) {
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
pack.loaded = true;
|
|
|
|
pack.error = true;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.onFileError.dispatch(pack.key, pack);
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
console.warn("Phaser.Loader error loading pack file: " + pack.key + ' from URL ' + pack.url);
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.nextPack(pack, false);
|
2014-05-29 14:57:01 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle loading the next asset pack.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#nextPack
|
|
|
|
* @private
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {object} pack - Pack associated with this request
|
|
|
|
* @param {?XMLHttpRequest} xhr
|
2014-05-29 14:57:01 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
nextPack: function (pack, success) {
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.onPackComplete.dispatch(pack.key, success, this.totalLoadedPacks(), this._packList.length);
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (this._packList.length)
|
2014-05-29 14:57:01 +00:00
|
|
|
{
|
|
|
|
this.loadPack();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.beginLoad();
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
|
|
|
* Load files. Private method ONLY used by loader.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#loadFile
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
loadFile: function () {
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
var file = this._fileList.shift();
|
|
|
|
|
|
|
|
if (!file)
|
2013-11-28 14:22:47 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
console.warn('Phaser.Loader loadFile: no more files');
|
2013-11-28 14:22:47 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-09-23 15:21:29 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
var _this = this;
|
|
|
|
|
2014-09-18 23:22:00 +00:00
|
|
|
this.onFileStart.dispatch(this.progress, file.key, file.url);
|
2014-04-14 10:51:50 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
// Image or Data?
|
|
|
|
switch (file.type)
|
|
|
|
{
|
|
|
|
case 'image':
|
|
|
|
case 'spritesheet':
|
|
|
|
case 'textureatlas':
|
|
|
|
case 'bitmapfont':
|
|
|
|
file.data = new Image();
|
|
|
|
file.data.name = file.key;
|
|
|
|
file.data.onload = function () {
|
2014-11-21 01:51:30 +00:00
|
|
|
file.data.onload = null;
|
|
|
|
file.data.onerror = null;
|
|
|
|
return _this.fileComplete(file);
|
2013-11-25 04:40:04 +00:00
|
|
|
};
|
|
|
|
file.data.onerror = function () {
|
2014-11-21 01:51:30 +00:00
|
|
|
file.data.onload = null;
|
|
|
|
file.data.onerror = null;
|
|
|
|
return _this.fileError(file);
|
2013-11-25 04:40:04 +00:00
|
|
|
};
|
2014-02-11 13:23:54 +00:00
|
|
|
if (this.crossOrigin)
|
|
|
|
{
|
|
|
|
file.data.crossOrigin = this.crossOrigin;
|
|
|
|
}
|
2013-11-25 04:40:04 +00:00
|
|
|
file.data.src = this.baseURL + file.url;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'audio':
|
|
|
|
file.url = this.getAudioURL(file.url);
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (file.url)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
|
|
|
// WebAudio or Audio Tag?
|
|
|
|
if (this.game.sound.usingWebAudio)
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(file, this.baseURL + file.url, 'arraybuffer', 'fileComplete', 'fileError');
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else if (this.game.sound.usingAudioTag)
|
|
|
|
{
|
|
|
|
if (this.game.sound.touchLocked)
|
|
|
|
{
|
|
|
|
// If audio is locked we can't do this yet, so need to queue this load request. Bum.
|
|
|
|
file.data = new Audio();
|
|
|
|
file.data.name = file.key;
|
|
|
|
file.data.preload = 'auto';
|
|
|
|
file.data.src = this.baseURL + file.url;
|
2014-11-21 01:51:30 +00:00
|
|
|
this.fileComplete(file);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file.data = new Audio();
|
|
|
|
file.data.name = file.key;
|
|
|
|
file.data.onerror = function () {
|
2014-11-21 01:51:30 +00:00
|
|
|
file.data.onerror = null;
|
|
|
|
return _this.fileError(file);
|
2013-11-25 04:40:04 +00:00
|
|
|
};
|
|
|
|
file.data.preload = 'auto';
|
|
|
|
file.data.src = this.baseURL + file.url;
|
2014-11-21 01:51:30 +00:00
|
|
|
file.data.addEventListener('canplaythrough', function ev () {
|
|
|
|
file.data.removeEventListener('canplaythrough', ev, false);
|
|
|
|
// Why does this cycle through games?
|
|
|
|
Phaser.GAMES[_this.game.id].load.fileComplete(file);
|
|
|
|
}, false);
|
2013-11-25 04:40:04 +00:00
|
|
|
file.data.load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
this.fileError(file);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-02-21 18:48:06 +00:00
|
|
|
case 'json':
|
2014-04-29 13:41:26 +00:00
|
|
|
|
2014-08-28 00:23:45 +00:00
|
|
|
if (this.useXDomainRequest && window.XDomainRequest)
|
2014-04-29 13:41:26 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
var xhr = new window.XDomainRequest();
|
2014-04-29 13:41:26 +00:00
|
|
|
|
2014-08-28 04:24:57 +00:00
|
|
|
// XDomainRequest has a few quirks. Occasionally it will abort requests
|
2014-04-29 13:41:26 +00:00
|
|
|
// A way to avoid this is to make sure ALL callbacks are set even if not used
|
|
|
|
// More info here: http://stackoverflow.com/questions/15786966/xdomainrequest-aborts-post-on-ie-9
|
2014-11-21 01:51:30 +00:00
|
|
|
xhr.timeout = 3000;
|
2014-04-29 13:41:26 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
xhr.onerror = function () {
|
|
|
|
return _this.dataLoadError(file, xhr);
|
2014-04-29 13:41:26 +00:00
|
|
|
};
|
2014-09-23 15:21:29 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
xhr.ontimeout = function () {
|
|
|
|
return _this.dataLoadError(file, xhr);
|
2014-04-29 13:41:26 +00:00
|
|
|
};
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
xhr.onprogress = function() {};
|
2014-04-29 13:41:26 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
xhr.onload = function(){
|
|
|
|
return _this.jsonLoadComplete(file, xhr);
|
2014-04-29 13:41:26 +00:00
|
|
|
};
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
xhr.open('GET', this.baseURL + file.url, true);
|
2014-04-29 13:41:26 +00:00
|
|
|
|
2014-09-23 15:21:29 +00:00
|
|
|
// Note: The xdr.send() call is wrapped in a timeout to prevent an issue with the interface where some requests are lost
|
2014-08-28 04:24:57 +00:00
|
|
|
// if multiple XDomainRequests are being sent at the same time.
|
|
|
|
setTimeout(function () {
|
2014-11-21 01:51:30 +00:00
|
|
|
xhr.send();
|
2014-08-28 04:24:57 +00:00
|
|
|
}, 0);
|
2014-04-29 13:41:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(file, this.baseURL + file.url, 'text', 'jsonLoadComplete', 'dataLoadError');
|
2014-04-29 13:41:26 +00:00
|
|
|
}
|
|
|
|
|
2014-02-21 18:48:06 +00:00
|
|
|
break;
|
|
|
|
|
2014-09-18 23:22:00 +00:00
|
|
|
case 'xml':
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(file, this.baseURL + file.url, 'text', 'xmlLoadComplete', 'dataLoadError');
|
2014-09-18 23:22:00 +00:00
|
|
|
break;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
case 'tilemap':
|
|
|
|
|
2013-11-28 14:22:47 +00:00
|
|
|
if (file.format === Phaser.Tilemap.TILED_JSON)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(file, this.baseURL + file.url, 'text', 'jsonLoadComplete', 'dataLoadError');
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-11-28 14:22:47 +00:00
|
|
|
else if (file.format === Phaser.Tilemap.CSV)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(file, this.baseURL + file.url, 'text', 'csvLoadComplete', 'dataLoadError');
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Error("Phaser.Loader. Invalid Tilemap format: " + file.format);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'text':
|
2013-12-20 18:27:36 +00:00
|
|
|
case 'script':
|
2014-02-14 23:51:49 +00:00
|
|
|
case 'physics':
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(file, this.baseURL + file.url, 'text', 'fileComplete', 'fileError');
|
2013-11-25 04:40:04 +00:00
|
|
|
break;
|
2013-11-28 05:43:35 +00:00
|
|
|
|
2013-12-20 18:27:36 +00:00
|
|
|
case 'binary':
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(file, this.baseURL + file.url, 'arraybuffer', 'fileComplete', 'fileError');
|
2013-11-28 05:43:35 +00:00
|
|
|
break;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2014-05-29 16:05:13 +00:00
|
|
|
/**
|
|
|
|
* Starts the xhr loader.
|
2014-09-23 15:21:29 +00:00
|
|
|
*
|
2014-05-29 16:05:13 +00:00
|
|
|
* @method Phaser.Loader#xhrLoad
|
|
|
|
* @private
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {object} data - Data to associate with the load/error handler. This is normally the file entry from the file list.
|
2014-05-29 16:05:13 +00:00
|
|
|
* @param {string} url - The URL of the file.
|
|
|
|
* @param {string} type - The xhr responseType.
|
|
|
|
* @param {string} onload - A String of the name of the local function to be called on a successful file load.
|
|
|
|
* @param {string} onerror - A String of the name of the local function to be called on a file load error.
|
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
xhrLoad: function (data, url, type, onload, onerror) {
|
2014-05-29 14:57:01 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
xhr.open("GET", url, true);
|
|
|
|
xhr.responseType = type;
|
2014-05-29 14:57:01 +00:00
|
|
|
|
|
|
|
var _this = this;
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
xhr.onload = function () {
|
|
|
|
return _this[onload](data, xhr);
|
2014-05-29 14:57:01 +00:00
|
|
|
};
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
xhr.onerror = function () {
|
|
|
|
return _this[onerror](data, xhr);
|
2014-05-29 14:57:01 +00:00
|
|
|
};
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
xhr.send();
|
2014-05-29 14:57:01 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
/**
|
2014-11-21 01:51:30 +00:00
|
|
|
* Give a bunch of URLs, return the first URL that has an extension this device thinks it can play.
|
2014-09-23 15:21:29 +00:00
|
|
|
*
|
2013-11-25 04:40:04 +00:00
|
|
|
* @method Phaser.Loader#getAudioURL
|
|
|
|
* @private
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {string[]|string} urls - Either an array of audio file URLs or a string containing a single URL path.
|
|
|
|
* @return {string} The URL to try and fetch; or null.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
|
|
|
getAudioURL: function (urls) {
|
|
|
|
|
|
|
|
if (typeof urls === 'string') { urls = [urls]; }
|
|
|
|
|
|
|
|
for (var i = 0; i < urls.length; i++)
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
var extension = urls[i].toLowerCase();
|
2013-11-25 04:40:04 +00:00
|
|
|
extension = extension.substr((Math.max(0, extension.lastIndexOf(".")) || Infinity) + 1);
|
|
|
|
|
2014-10-17 17:50:12 +00:00
|
|
|
if (extension.indexOf("?") >= 0)
|
|
|
|
{
|
|
|
|
extension = extension.substr(0, extension.indexOf("?"));
|
|
|
|
}
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
if (this.game.device.canPlayAudio(extension))
|
|
|
|
{
|
|
|
|
return urls[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2013-11-26 15:29:03 +00:00
|
|
|
* Error occured when loading a file.
|
2013-11-25 04:40:04 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Loader#fileError
|
2014-11-21 01:51:30 +00:00
|
|
|
* @private
|
|
|
|
* @param {object} file
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
fileError: function (file) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
file.loaded = true;
|
|
|
|
file.error = true;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.onFileError.dispatch(file.key, file);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
console.warn("Phaser.Loader error loading file: " + file.key + ' from URL ' + file.url);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.nextFile(file, false);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Called when a file is successfully loaded.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#fileComplete
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {object} file - File loaded
|
|
|
|
* @param {XMLHttpRequest} xhr
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
fileComplete: function (file, xhr) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-11-28 15:57:09 +00:00
|
|
|
file.loaded = true;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
var loadNext = true;
|
|
|
|
|
|
|
|
switch (file.type)
|
|
|
|
{
|
|
|
|
case 'image':
|
|
|
|
|
|
|
|
this.game.cache.addImage(file.key, file.url, file.data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'spritesheet':
|
|
|
|
|
2013-12-13 14:04:14 +00:00
|
|
|
this.game.cache.addSpriteSheet(file.key, file.url, file.data, file.frameWidth, file.frameHeight, file.frameMax, file.margin, file.spacing);
|
2013-11-25 04:40:04 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'textureatlas':
|
|
|
|
|
|
|
|
if (file.atlasURL == null)
|
|
|
|
{
|
|
|
|
this.game.cache.addTextureAtlas(file.key, file.url, file.data, file.atlasData, file.format);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Load the JSON or XML before carrying on with the next file
|
|
|
|
loadNext = false;
|
|
|
|
|
|
|
|
if (file.format == Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY || file.format == Phaser.Loader.TEXTURE_ATLAS_JSON_HASH)
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(file, this.baseURL + file.atlasURL, 'text', 'jsonLoadComplete', 'dataLoadError');
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else if (file.format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING)
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(file, this.baseURL + file.atlasURL, 'text', 'xmlLoadComplete', 'dataLoadError');
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw new Error("Phaser.Loader. Invalid Texture Atlas format: " + file.format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'bitmapfont':
|
|
|
|
|
|
|
|
if (file.xmlURL == null)
|
|
|
|
{
|
2014-02-14 04:34:57 +00:00
|
|
|
this.game.cache.addBitmapFont(file.key, file.url, file.data, file.xmlData, file.xSpacing, file.ySpacing);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Load the XML before carrying on with the next file
|
|
|
|
loadNext = false;
|
2014-11-21 01:51:30 +00:00
|
|
|
this.xhrLoad(file, this.baseURL + file.xmlURL, 'text', 'xmlLoadComplete', 'dataLoadError');
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'audio':
|
|
|
|
|
|
|
|
if (this.game.sound.usingWebAudio)
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
file.data = xhr.response;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
this.game.cache.addSound(file.key, file.url, file.data, true, false);
|
|
|
|
|
|
|
|
if (file.autoDecode)
|
|
|
|
{
|
|
|
|
var that = this;
|
|
|
|
var key = file.key;
|
|
|
|
|
2014-03-23 08:33:53 +00:00
|
|
|
this.game.cache.updateSound(key, 'isDecoding', true);
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.game.sound.context.decodeAudioData(file.data, function (buffer) {
|
|
|
|
if (buffer)
|
|
|
|
{
|
2013-11-27 16:33:49 +00:00
|
|
|
that.game.cache.decodedSound(key, buffer);
|
2013-12-13 14:04:14 +00:00
|
|
|
that.game.sound.onSoundDecode.dispatch(key, that.game.cache.getSound(key));
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.game.cache.addSound(file.key, file.url, file.data, false, true);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'text':
|
2014-11-21 01:51:30 +00:00
|
|
|
file.data = xhr.responseText;
|
2013-11-25 04:40:04 +00:00
|
|
|
this.game.cache.addText(file.key, file.url, file.data);
|
|
|
|
break;
|
2013-11-28 05:43:35 +00:00
|
|
|
|
2014-02-14 23:51:49 +00:00
|
|
|
case 'physics':
|
2014-11-21 01:51:30 +00:00
|
|
|
var data = JSON.parse(this.responseText);
|
2014-02-14 23:51:49 +00:00
|
|
|
this.game.cache.addPhysicsData(file.key, file.url, data, file.format);
|
|
|
|
break;
|
|
|
|
|
2013-11-28 05:43:35 +00:00
|
|
|
case 'script':
|
|
|
|
file.data = document.createElement('script');
|
|
|
|
file.data.language = 'javascript';
|
|
|
|
file.data.type = 'text/javascript';
|
2013-11-29 12:26:53 +00:00
|
|
|
file.data.defer = false;
|
2014-11-21 01:51:30 +00:00
|
|
|
file.data.text = this.responseText;
|
2013-11-28 05:43:35 +00:00
|
|
|
document.head.appendChild(file.data);
|
2014-02-26 02:45:06 +00:00
|
|
|
if (file.callback)
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
file.data = file.callback.call(file.callbackContext, file.key, this.responseText);
|
2014-02-26 02:45:06 +00:00
|
|
|
}
|
2013-11-28 05:43:35 +00:00
|
|
|
break;
|
2013-12-20 18:27:36 +00:00
|
|
|
|
|
|
|
case 'binary':
|
|
|
|
if (file.callback)
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
file.data = file.callback.call(file.callbackContext, file.key, this.response);
|
2013-12-20 18:27:36 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
file.data = xhr.response;
|
2013-12-20 18:27:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.game.cache.addBinary(file.key, file.data);
|
|
|
|
|
|
|
|
break;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (loadNext)
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
this.nextFile(file, true);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Successfully loaded a JSON file.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#jsonLoadComplete
|
2014-11-21 01:51:30 +00:00
|
|
|
* @private
|
|
|
|
* @param {object} file - File associated with this request
|
|
|
|
* @param {XMLHttpRequest} xhr
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
jsonLoadComplete: function (file, xhr) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
var data = JSON.parse(xhr.responseText);
|
2013-11-28 14:22:47 +00:00
|
|
|
|
|
|
|
file.loaded = true;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-11-28 14:22:47 +00:00
|
|
|
if (file.type === 'tilemap')
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
|
|
|
this.game.cache.addTilemap(file.key, file.url, data, file.format);
|
|
|
|
}
|
2014-02-21 18:48:06 +00:00
|
|
|
else if (file.type === 'json')
|
|
|
|
{
|
|
|
|
this.game.cache.addJSON(file.key, file.url, data);
|
|
|
|
}
|
2013-11-25 04:40:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this.game.cache.addTextureAtlas(file.key, file.url, file.data, data, file.format);
|
|
|
|
}
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.nextFile(file, true);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Successfully loaded a CSV file.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#csvLoadComplete
|
2014-11-21 01:51:30 +00:00
|
|
|
* @private
|
|
|
|
* @param {object} file - File associated with this request
|
|
|
|
* @param {XMLHttpRequest} xhr
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
csvLoadComplete: function (file, xhr) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
var data = xhr.responseText;
|
2013-11-28 14:22:47 +00:00
|
|
|
|
|
|
|
file.loaded = true;
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
this.game.cache.addTilemap(file.key, file.url, data, file.format);
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.nextFile(file, true);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Error occured when load a JSON.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#dataLoadError
|
2014-11-21 01:51:30 +00:00
|
|
|
* @private
|
|
|
|
* @param {object} file - File associated with this request
|
|
|
|
* @param {XMLHttpRequest} xhr
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
dataLoadError: function (file, xhr) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-11-28 14:22:47 +00:00
|
|
|
file.loaded = true;
|
2013-11-25 04:40:04 +00:00
|
|
|
file.error = true;
|
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
console.warn("Phaser.Loader dataLoadError: " + file.key);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.nextFile(file, true);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Successfully loaded an XML file.
|
|
|
|
*
|
|
|
|
* @method Phaser.Loader#xmlLoadComplete
|
2014-11-21 01:51:30 +00:00
|
|
|
* @private
|
|
|
|
* @param {object} file - File associated with this request
|
|
|
|
* @param {XMLHttpRequest} xhr
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
xmlLoadComplete: function (file, xhr) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
file.loaded = true;
|
|
|
|
|
|
|
|
if (xhr.responseType !== '' && xhr.responseType !== 'text')
|
2014-09-18 23:22:00 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
console.warn('Invalid XML Response Type', file);
|
|
|
|
console.warn(xhr);
|
2014-09-18 23:22:00 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
var data = xhr.responseText;
|
|
|
|
var xml = this.parseXml(data);
|
|
|
|
|
|
|
|
file.error = true; // until make it past xml exception (why exception?)
|
|
|
|
|
|
|
|
if (!xml)
|
|
|
|
{
|
|
|
|
throw new Error("Phaser.Loader. Invalid XML");
|
|
|
|
}
|
|
|
|
|
|
|
|
file.error = false;
|
|
|
|
|
|
|
|
if (file.type === 'bitmapfont')
|
|
|
|
{
|
|
|
|
this.game.cache.addBitmapFont(file.key, file.url, file.data, xml, file.xSpacing, file.ySpacing);
|
|
|
|
}
|
|
|
|
else if (file.type === 'textureatlas')
|
|
|
|
{
|
|
|
|
this.game.cache.addTextureAtlas(file.key, file.url, file.data, xml, file.format);
|
|
|
|
}
|
|
|
|
else if (file.type === 'xml')
|
|
|
|
{
|
|
|
|
this.game.cache.addXML(file.key, file.url, xml);
|
|
|
|
}
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
this.nextFile(file, true);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses string data as XML.
|
|
|
|
*
|
|
|
|
* @method parseXml
|
|
|
|
* @private
|
|
|
|
* @param {string} data - The XML text to parse
|
|
|
|
* @return {?XMLDocument} Returns the xml document, or null if such could not parsed to a valid document.
|
|
|
|
*/
|
|
|
|
parseXml: function (data) {
|
|
|
|
|
|
|
|
var xml;
|
2013-11-25 04:40:04 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
if (window['DOMParser'])
|
|
|
|
{
|
|
|
|
var domparser = new DOMParser();
|
|
|
|
xml = domparser.parseFromString(data, "text/xml");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
xml = new ActiveXObject("Microsoft.XMLDOM");
|
|
|
|
xml.async = 'false';
|
|
|
|
xml.loadXML(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e)
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
xml = null;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!xml || !xml.documentElement || xml.getElementsByTagName("parsererror").length)
|
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
return null;
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2014-11-21 01:51:30 +00:00
|
|
|
else
|
2014-09-18 23:22:00 +00:00
|
|
|
{
|
2014-11-21 01:51:30 +00:00
|
|
|
return xml;
|
2014-09-18 23:22:00 +00:00
|
|
|
}
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle loading next file.
|
|
|
|
*
|
2014-05-29 16:05:13 +00:00
|
|
|
* @method Phaser.Loader#nextFile
|
|
|
|
* @private
|
2014-11-21 01:51:30 +00:00
|
|
|
* @param {object} previousFile
|
2013-11-26 15:29:03 +00:00
|
|
|
* @param {boolean} success - Whether the previous asset loaded successfully or not.
|
2013-11-25 04:40:04 +00:00
|
|
|
*/
|
2014-11-21 01:51:30 +00:00
|
|
|
nextFile: function (previousFile, success) {
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2013-12-31 17:35:40 +00:00
|
|
|
this.progressFloat += this._progressChunk;
|
|
|
|
this.progress = Math.round(this.progressFloat);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
|
|
|
if (this.progress > 100)
|
|
|
|
{
|
|
|
|
this.progress = 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.preloadSprite !== null)
|
|
|
|
{
|
|
|
|
if (this.preloadSprite.direction === 0)
|
|
|
|
{
|
2014-03-18 16:23:44 +00:00
|
|
|
this.preloadSprite.rect.width = Math.floor((this.preloadSprite.width / 100) * this.progress);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-03-18 16:23:44 +00:00
|
|
|
this.preloadSprite.rect.height = Math.floor((this.preloadSprite.height / 100) * this.progress);
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2014-09-09 11:48:38 +00:00
|
|
|
|
|
|
|
this.preloadSprite.sprite.updateCrop();
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
var totalProcessed = this._loadedFileCount + this._failedFileCount;
|
|
|
|
this.onFileComplete.dispatch(this.progress, previousFile.key, success, totalProcessed, this._totalFileCount);
|
2013-11-25 04:40:04 +00:00
|
|
|
|
2014-11-21 01:51:30 +00:00
|
|
|
if (this._fileList.length)
|
2013-11-25 04:40:04 +00:00
|
|
|
{
|
|
|
|
this.loadFile();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.hasLoaded = true;
|
|
|
|
this.isLoading = false;
|
2014-03-23 07:59:28 +00:00
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
this.removeAll();
|
|
|
|
|
|
|
|
this.onLoadComplete.dispatch();
|
|
|
|
}
|
|
|
|
|
2013-11-26 15:29:03 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of files that have already been loaded, even if they errored.
|
|
|
|
*
|
2014-05-29 16:05:13 +00:00
|
|
|
* @method Phaser.Loader#totalLoadedFiles
|
2013-11-26 15:29:03 +00:00
|
|
|
* @return {number} The number of files that have already been loaded (even if they errored)
|
|
|
|
*/
|
|
|
|
totalLoadedFiles: function () {
|
|
|
|
|
|
|
|
var total = 0;
|
|
|
|
|
2013-11-26 21:10:01 +00:00
|
|
|
for (var i = 0; i < this._fileList.length; i++)
|
2013-11-26 15:29:03 +00:00
|
|
|
{
|
|
|
|
if (this._fileList[i].loaded)
|
|
|
|
{
|
|
|
|
total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2014-05-29 14:57:01 +00:00
|
|
|
* Returns the number of files still waiting to be processed in the load queue. This value decreases as each file in the queue is loaded.
|
2013-11-26 15:29:03 +00:00
|
|
|
*
|
2014-05-29 16:05:13 +00:00
|
|
|
* @method Phaser.Loader#totalQueuedFiles
|
2013-11-26 15:29:03 +00:00
|
|
|
* @return {number} The number of files that still remain in the load queue.
|
|
|
|
*/
|
|
|
|
totalQueuedFiles: function () {
|
|
|
|
|
|
|
|
var total = 0;
|
|
|
|
|
2013-11-26 21:10:01 +00:00
|
|
|
for (var i = 0; i < this._fileList.length; i++)
|
2013-11-26 15:29:03 +00:00
|
|
|
{
|
|
|
|
if (this._fileList[i].loaded === false)
|
|
|
|
{
|
|
|
|
total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
|
2014-05-29 14:57:01 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of asset packs that have already been loaded, even if they errored.
|
|
|
|
*
|
2014-05-29 16:05:13 +00:00
|
|
|
* @method Phaser.Loader#totalLoadedPacks
|
2014-05-29 14:57:01 +00:00
|
|
|
* @return {number} The number of asset packs that have already been loaded (even if they errored)
|
|
|
|
*/
|
|
|
|
totalLoadedPacks: function () {
|
|
|
|
|
|
|
|
var total = 0;
|
|
|
|
|
|
|
|
for (var i = 0; i < this._packList.length; i++)
|
|
|
|
{
|
|
|
|
if (this._packList[i].loaded)
|
|
|
|
{
|
|
|
|
total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the number of asset packs still waiting to be processed in the load queue. This value decreases as each pack in the queue is loaded.
|
|
|
|
*
|
2014-05-29 16:05:13 +00:00
|
|
|
* @method Phaser.Loader#totalQueuedPacks
|
2014-05-29 14:57:01 +00:00
|
|
|
* @return {number} The number of asset packs that still remain in the load queue.
|
|
|
|
*/
|
|
|
|
totalQueuedPacks: function () {
|
|
|
|
|
|
|
|
var total = 0;
|
|
|
|
|
|
|
|
for (var i = 0; i < this._packList.length; i++)
|
|
|
|
{
|
|
|
|
if (this._packList[i].loaded === false)
|
|
|
|
{
|
|
|
|
total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
|
2013-11-25 04:40:04 +00:00
|
|
|
}
|
2013-08-28 06:02:55 +00:00
|
|
|
|
2013-11-19 23:12:37 +00:00
|
|
|
};
|
2013-12-30 16:54:00 +00:00
|
|
|
|
|
|
|
Phaser.Loader.prototype.constructor = Phaser.Loader;
|