UNPKG

phaser-ce

Version:

Phaser CE (Community Edition) is a fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers.

1,212 lines (1,058 loc) 126 kB
/* jshint wsh:true */ /** * @author Richard Davey <rich@photonstorm.com> * @copyright 2016 Photon Storm Ltd. * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} */ /** * The Loader handles loading all external content such as Images, Sounds, Texture Atlases and data files. * * The loader uses a combination of tag loading (eg. Image elements) and XHR and provides progress and completion callbacks. * * Parallel loading (see {@link #enableParallel}) is supported and enabled by default. * Load-before behavior of parallel resources is controlled by synchronization points as discussed in {@link #withSyncPoint}. * * Texture Atlases can be created with tools such as [Texture Packer](https://www.codeandweb.com/texturepacker/phaser) and * [Shoebox](http://renderhjs.net/shoebox/) * * @class Phaser.Loader * @param {Phaser.Game} game - A reference to the currently running game. */ Phaser.Loader = function (game) { /** * Local reference to game. * @property {Phaser.Game} game * @protected */ this.game = game; /** * Local reference to the Phaser.Cache. * @property {Phaser.Cache} cache * @protected */ this.cache = game.cache; /** * 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. * @property {boolean} resetLocked * @default */ this.resetLocked = false; /** * True if the Loader is in the process of loading the queue. * @property {boolean} isLoading * @default */ this.isLoading = false; /** * True if all assets in the queue have finished loading. * @property {boolean} hasLoaded * @default */ this.hasLoaded = false; /** * You can optionally link a progress sprite with {@link Phaser.Loader#setPreloadSprite setPreloadSprite}. * * This property is an object containing: sprite, rect, direction, width and height * * @property {?object} preloadSprite * @protected */ this.preloadSprite = null; /** * The crossOrigin value applied to loaded images. Very often this needs to be set to 'anonymous'. * @property {boolean|string} crossOrigin * @default */ this.crossOrigin = false; /** * If you want to append a URL before the path of any asset you can set this here. * Useful if allowing the asset base url to be configured outside of the game code. * The string _must_ end with a "/". * * @property {string} baseURL */ this.baseURL = ''; /** * The value of `path`, if set, is placed before any _relative_ file path given. For example: * * `load.path = "images/sprites/"; * load.image("ball", "ball.png"); * load.image("tree", "level1/oaktree.png"); * load.image("boom", "http://server.com/explode.png");` * * Would load the `ball` file from `images/sprites/ball.png` and the tree from * `images/sprites/level1/oaktree.png` but the file `boom` would load from the URL * given as it's an absolute URL. * * Please note that the path is added before the filename but *after* the baseURL (if set.) * * The string _must_ end with a "/". * * @property {string} path */ this.path = ''; /** * Used to map the application mime-types to to the Accept header in XHR requests. * If you don't require these mappings, or they cause problems on your server, then * remove them from the headers object and the XHR request will not try to use them. * * This object can also be used to set the `X-Requested-With` header to * `XMLHttpRequest` (or any other value you need). To enable this do: * * `this.load.headers.requestedWith = 'XMLHttpRequest'` * * before adding anything to the Loader. The XHR loader will then call: * * `setRequestHeader('X-Requested-With', this.headers['requestedWith'])` * * @property {object} headers * @default */ this.headers = { "requestedWith": false, "json": "application/json", "xml": "application/xml" }; /** * This event is dispatched when the loading process starts: before the first file has been requested, * but after all the initial packs have been loaded. * * @property {Phaser.Signal} onLoadStart */ this.onLoadStart = new Phaser.Signal(); /** * This event is dispatched when the final file in the load queue has either loaded or failed. * * @property {Phaser.Signal} onLoadComplete */ this.onLoadComplete = new Phaser.Signal(); /** * This event is dispatched when an asset pack has either loaded or failed to load. * * This is called when the asset pack manifest file has loaded and successfully added its contents to the loader queue. * * Params: `(pack key, success?, total packs loaded, total packs)` * * @property {Phaser.Signal} onPackComplete */ this.onPackComplete = new Phaser.Signal(); /** * This event is dispatched immediately before a file starts loading. * It's possible the file may fail (eg. download error, invalid format) after this event is sent. * * Params: `(progress, file key, file url)` * * @property {Phaser.Signal} onFileStart */ this.onFileStart = new Phaser.Signal(); /** * This event is dispatched when a file has either loaded or failed to load. * * Any function bound to this will receive the following parameters: * * progress, file key, success?, total loaded files, total files * * Where progress is a number between 1 and 100 (inclusive) representing the percentage of the load. * * @property {Phaser.Signal} onFileComplete */ this.onFileComplete = new Phaser.Signal(); /** * This event is dispatched when a file (or pack) errors as a result of the load request. * * For files it will be triggered before `onFileComplete`. For packs it will be triggered before `onPackComplete`. * * Params: `(file key, file)` * * @property {Phaser.Signal} onFileError */ this.onFileError = new Phaser.Signal(); /** * If true and if the browser supports XDomainRequest, it will be used in preference for XHR. * * This is only relevant for IE 9 and should _only_ be enabled for IE 9 clients when required by the server/CDN. * * @property {boolean} useXDomainRequest * @deprecated This is only relevant for IE 9. */ this.useXDomainRequest = false; /** * @private * @property {boolean} _warnedAboutXDomainRequest - Control number of warnings for using XDR outside of IE 9. */ this._warnedAboutXDomainRequest = false; /** * If true (the default) then parallel downloading will be enabled. * * To disable all parallel downloads this must be set to false prior to any resource being loaded. * * @property {boolean} enableParallel */ this.enableParallel = true; /** * The number of concurrent / parallel resources to try and fetch at once. * * Many current browsers limit 6 requests per domain; this is slightly conservative. * * This should generally be left at the default, but can be set to a higher limit for specific use-cases. Just be careful when setting large values as different browsers could behave differently. * * @property {integer} maxParallelDownloads */ this.maxParallelDownloads = 4; /** * A counter: if more than zero, files will be automatically added as a synchronization point. * @property {integer} _withSyncPointDepth; */ this._withSyncPointDepth = 0; /** * Contains all the information for asset files (including packs) to load. * * File/assets are only removed from the list after all loading completes. * * @property {file[]} _fileList * @private */ this._fileList = []; /** * Inflight files (or packs) that are being fetched/processed. * * This means that if there are any files in the flight queue there should still be processing * going on; it should only be empty before or after loading. * * The files in the queue may have additional properties added to them, * including `requestObject` which is normally the associated XHR. * * @property {file[]} _flightQueue * @private */ this._flightQueue = []; /** * The offset into the fileList past all the complete (loaded or error) entries. * * @property {integer} _processingHead * @private */ this._processingHead = 0; /** * True when the first file (not pack) has loading started. * This used to to control dispatching `onLoadStart` which happens after any initial packs are loaded. * * @property {boolean} _initialPacksLoaded * @private */ this._fileLoadStarted = false; /** * Total packs seen - adjusted when a pack is added. * @property {integer} _totalPackCount * @private */ this._totalPackCount = 0; /** * Total files seen - adjusted when a file is added. * @property {integer} _totalFileCount * @private */ this._totalFileCount = 0; /** * Total packs loaded - adjusted just prior to `onPackComplete`. * @property {integer} _loadedPackCount * @private */ this._loadedPackCount = 0; /** * Total files loaded - adjusted just prior to `onFileComplete`. * @property {integer} _loadedFileCount * @private */ this._loadedFileCount = 0; }; /** * @constant * @type {number} */ Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY = 0; /** * @constant * @type {number} */ Phaser.Loader.TEXTURE_ATLAS_JSON_HASH = 1; /** * @constant * @type {number} */ Phaser.Loader.TEXTURE_ATLAS_XML_STARLING = 2; /** * @constant * @type {number} */ Phaser.Loader.PHYSICS_LIME_CORONA_JSON = 3; /** * @constant * @type {number} */ Phaser.Loader.PHYSICS_PHASER_JSON = 4; /** * @constant * @type {number} */ Phaser.Loader.TEXTURE_ATLAS_JSON_PYXEL = 5; Phaser.Loader.prototype = { /** * 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. * This allows you to easily make loading bars for games. * * The sprite will automatically be made visible when calling this. * * @method Phaser.Loader#setPreloadSprite * @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. */ setPreloadSprite: function (sprite, direction) { direction = direction || 0; this.preloadSprite = { sprite: sprite, direction: direction, width: sprite.width, height: sprite.height, rect: null }; if (direction === 0) { // Horizontal rect this.preloadSprite.rect = new Phaser.Rectangle(0, 0, 1, sprite.height); } else { // Vertical rect this.preloadSprite.rect = new Phaser.Rectangle(0, 0, sprite.width, 1); } sprite.crop(this.preloadSprite.rect); sprite.visible = true; }, /** * Called automatically by ScaleManager when the game resizes in RESIZE scalemode. * * This can be used to adjust the preloading sprite size, eg. * * @method Phaser.Loader#resize * @protected */ resize: function () { if (this.preloadSprite && this.preloadSprite.height !== this.preloadSprite.sprite.height) { this.preloadSprite.rect.height = this.preloadSprite.sprite.height; } }, /** * Check whether a file/asset with a specific key is queued to be loaded. * * To access a loaded asset use Phaser.Cache, eg. {@link Phaser.Cache#checkImageKey} * * @method Phaser.Loader#checkKeyExists * @param {string} type - The type asset you want to check. * @param {string} key - Key of the asset you want to check. * @return {boolean} Return true if exists, otherwise return false. */ checkKeyExists: function (type, key) { return this.getAssetIndex(type, key) > -1; }, /** * Get the queue-index of the file/asset with a specific key. * * Only assets in the download file queue will be found. * * @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. * The index may change and should only be used immediately following this call */ getAssetIndex: function (type, key) { var bestFound = -1; for (var i = 0; i < this._fileList.length; i++) { var file = this._fileList[i]; if (file.type === type && file.key === key) { bestFound = i; // An already loaded/loading file may be superceded. if (!file.loaded && !file.loading) { break; } } } return bestFound; }, /** * Find a file/asset with a specific key. * * Only assets in the download file queue will be found. * * @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. * @return {any} Returns an object if found that has 2 properties: `index` and `file`; otherwise a non-true value is returned. * The index may change and should only be used immediately following this call. */ getAsset: function (type, key) { var fileIndex = this.getAssetIndex(type, key); if (fileIndex > -1) { return { index: fileIndex, file: this._fileList[fileIndex] }; } return false; }, /** * Reset the loader and clear any queued assets. If `Loader.resetLocked` is true this operation will abort. * * This will abort any loading and clear any queued assets. * * Optionally you can clear any associated events. * * @method Phaser.Loader#reset * @protected * @param {boolean} [hard=false] - If true then the preload sprite and other artifacts may also be cleared. * @param {boolean} [clearEvents=false] - If true then the all Loader signals will have removeAll called on them. */ reset: function (hard, clearEvents) { if (clearEvents === undefined) { clearEvents = false; } if (this.resetLocked) { return; } if (hard) { this.preloadSprite = null; } this.isLoading = false; this._processingHead = 0; this._fileList.length = 0; this._flightQueue.length = 0; this._fileLoadStarted = false; this._totalFileCount = 0; this._totalPackCount = 0; this._loadedPackCount = 0; this._loadedFileCount = 0; if (clearEvents) { this.onLoadStart.removeAll(); this.onLoadComplete.removeAll(); this.onPackComplete.removeAll(); this.onFileStart.removeAll(); this.onFileComplete.removeAll(); this.onFileError.removeAll(); } }, /** * Internal function that adds a new entry to the file list. Do not call directly. * * @method Phaser.Loader#addToFileList * @protected * @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=(none)] - Any additional properties needed to load the file. These are added directly to the added file object and overwrite any defaults. * @param {boolean} [overwrite=false] - If true then this will overwrite a file asset of the same type/key. Otherwise it will only add a new asset. If overwrite is true, and the asset is already being loaded (or has been loaded), then it is appended instead. * @param {string} [extension] - If no URL is given the Loader will sometimes auto-generate the URL based on the key, using this as the extension. * @return {Phaser.Loader} This instance of the Phaser Loader. */ addToFileList: function (type, key, url, properties, overwrite, extension) { if (overwrite === undefined) { overwrite = false; } if (key === undefined || key === '') { console.warn("Phaser.Loader: Invalid or no key given of type " + type); return this; } if (url === undefined || url === null) { if (extension) { url = key + extension; } else { console.warn("Phaser.Loader: No URL given for file type: " + type + " key: " + key); return this; } } var file = { type: type, key: key, path: this.path, url: url, syncPoint: this._withSyncPointDepth > 0, data: null, loading: false, loaded: false, error: false }; if (properties) { for (var prop in properties) { file[prop] = properties[prop]; } } var fileIndex = this.getAssetIndex(type, key); if (overwrite && fileIndex > -1) { var currentFile = this._fileList[fileIndex]; if (!currentFile.loading && !currentFile.loaded) { this._fileList[fileIndex] = file; } else { this._fileList.push(file); this._totalFileCount++; } } else if (fileIndex === -1) { this._fileList.push(file); this._totalFileCount++; } return this; }, /** * Internal function that replaces an existing entry in the file list with a new one. Do not call directly. * * @method Phaser.Loader#replaceInFileList * @protected * @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. */ replaceInFileList: function (type, key, url, properties) { return this.addToFileList(type, key, url, properties, true); }, /** * Add a JSON resource pack ('packfile') to the Loader. * * A packfile is a JSON file that contains a list of assets to the be loaded. * Please see the example 'loader/asset pack' in the Phaser Examples repository. * * Packs are always put before the first non-pack file that is not loaded / loading. * * This means that all packs added before any loading has started are added to the front * of the file queue, in the order added. * * The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load. * * The URL of the packfile can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * @method Phaser.Loader#pack * @param {string} key - Unique asset key of this resource pack. * @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=(loader)] - 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 (url === undefined) { url = null; } if (data === undefined) { data = null; } if (callbackContext === undefined) { callbackContext = null; } if (!url && !data) { console.warn('Phaser.Loader.pack - Both url and data are null. One must be set.'); return this; } var pack = { type: 'packfile', key: key, url: url, path: this.path, syncPoint: true, data: null, loading: false, loaded: false, error: false, callbackContext: callbackContext }; // A data object has been given if (data) { if (typeof data === 'string') { data = JSON.parse(data); } pack.data = data || {}; // Already consider 'loaded' pack.loaded = true; } // Add before first non-pack/no-loaded ~ last pack from start prior to loading // (Read one past for splice-to-end) for (var i = 0; i < this._fileList.length + 1; i++) { var file = this._fileList[i]; if (!file || (!file.loaded && !file.loading && file.type !== 'packfile')) { this._fileList.splice(i, 0, pack); this._totalPackCount++; break; } } return this; }, /** * Adds an Image to the current load queue. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle. * * The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load. * * Retrieve the image via `Cache.getImage(key)` * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" * and no URL is given then the Loader will set the URL to be "alien.png". It will always add `.png` as the extension. * If you do not desire this action then provide a URL. * * This method also supports passing in a texture object as the `url` argument. This allows you to load * compressed textures into Phaser. You can also use `Loader.texture` to do this. * * Compressed Textures are a WebGL only feature, and require 3rd party tools to create. * Available tools include Texture Packer, PVRTexTool, DirectX Texture Tool and Mali Texture Compression Tool. * * Supported texture compression formats are: PVRTC, S3TC and ETC1. * Supported file formats are: PVR, DDS, KTX and PKM. * * The formats that support all 3 compression algorithms are PVR and KTX. * PKM only supports ETC1, and DDS only S3TC for now. * * The texture path object looks like this: * * ```javascript * load.image('factory', { * etc1: 'assets/factory_etc1.pkm', * s3tc: 'assets/factory_dxt1.pvr', * pvrtc: 'assets/factory_pvrtc.pvr', * truecolor: 'assets/factory.png' * }); * ``` * * The `truecolor` property points to a standard PNG file, that will be used if none of the * compressed formats are supported by the browser / GPU. * * @method Phaser.Loader#image * @param {string} key - Unique asset key of this image file. * @param {string|object} [url] - URL of an image file. If undefined or `null` the url will be set to `<key>.png`, i.e. if `key` was "alien" then the URL will be "alien.png". Can also be a texture data object. * @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. */ image: function (key, url, overwrite) { if (typeof url === 'object') { return this.texture(key, url, overwrite); } else { return this.addToFileList('image', key, url, undefined, overwrite, '.png'); } }, /** * Generate an image from a BitmapData object and add it to the current load queue. * * @method Phaser.Loader#imageFromBitmapData * @param {string} key - Unique asset key for the generated image. * @param {Phaser.BitmapData} bitmapData * @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. */ imageFromBitmapData: function (key, bitmapData, overwrite) { return this.image(key, bitmapData.canvas.toDataURL('image/png'), overwrite); }, /** * Adds a Compressed Texture Image to the current load queue. * * Compressed Textures are a WebGL only feature, and require 3rd party tools to create. * Available tools include Texture Packer, PVRTexTool, DirectX Texture Tool and Mali Texture Compression Tool. * * Supported texture compression formats are: PVRTC, S3TC and ETC1. * Supported file formats are: PVR, DDS, KTX and PKM. * * The formats that support all 3 compression algorithms are PVR and KTX. * PKM only supports ETC1, and DDS only S3TC for now. * * The texture path object looks like this: * * ```javascript * load.texture('factory', { * etc1: 'assets/factory_etc1.pkm', * s3tc: 'assets/factory_dxt1.pvr', * pvrtc: 'assets/factory_pvrtc.pvr', * truecolor: 'assets/factory.png' * }); * ``` * * The `truecolor` property points to a standard PNG file, that will be used if none of the * compressed formats are supported by the browser / GPU. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load. * * Retrieve the image via `Cache.getImage(key)` * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" * and no URL is given then the Loader will set the URL to be "alien.pvr". It will always add `.pvr` as the extension. * If you do not desire this action then provide a URL. * * @method Phaser.Loader#texture * @param {string} key - Unique asset key of this image file. * @param {object} object - The texture path data object. * @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. */ texture: function (key, object, overwrite) { if (this.game.renderType === Phaser.WEBGL) { var compression = this.game.renderer.extensions.compression; var exkey; for (exkey in object) { if (exkey.toUpperCase() in compression) { return this.addToFileList('texture', key, object[exkey], undefined, overwrite, '.pvr'); } } } // Check if we have a truecolor texture to fallback. // Also catches calls to this function that are from a Canvas renderer if (object['truecolor']) { this.addToFileList('image', key, object['truecolor'], undefined, overwrite, '.png'); } return this; }, /** * Adds an array of images to the current load queue. * * It works by passing each element of the array to the Loader.image method. * * The files are **not** loaded immediately after calling this method. The files are added to the queue ready to be loaded when the loader starts. * * Phaser can load all common image types: png, jpg, gif and any other format the browser can natively handle. * * The keys must be unique Strings. They are used to add the files to the Phaser.Cache upon successful load. * * Retrieve the images via `Cache.getImage(key)` * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" * and no URL is given then the Loader will set the URL to be "alien.png". It will always add `.png` as the extension. * If you do not desire this action then provide a URL. * * @method Phaser.Loader#images * @param {array} keys - An array of unique asset keys of the image files. * @param {array} [urls] - Optional array of URLs. If undefined or `null` the url will be set to `<key>.png`, i.e. if `key` was "alien" then the URL will be "alien.png". If provided the URLs array length must match the keys array length. * @return {Phaser.Loader} This Loader instance. */ images: function (keys, urls) { if (Array.isArray(urls)) { for (var i = 0; i < keys.length; i++) { this.image(keys[i], urls[i]); } } else { for (var i = 0; i < keys.length; i++) { this.image(keys[i]); } } return this; }, /** * Adds a Text file to the current load queue. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load. * * Retrieve the file via `Cache.getText(key)` * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" * and no URL is given then the Loader will set the URL to be "alien.txt". It will always add `.txt` as the extension. * If you do not desire this action then provide a URL. * * @method Phaser.Loader#text * @param {string} key - Unique asset key of the text file. * @param {string} [url] - URL of the text file. If undefined or `null` the url will be set to `<key>.txt`, i.e. if `key` was "alien" then the URL will be "alien.txt". * @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. */ text: function (key, url, overwrite) { return this.addToFileList('text', key, url, undefined, overwrite, '.txt'); }, /** * Adds a JSON file to the current load queue. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load. * * Retrieve the file via `Cache.getJSON(key)`. JSON files are automatically parsed upon load. * If you need to control when the JSON is parsed then use `Loader.text` instead and parse the text file as needed. * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" * and no URL is given then the Loader will set the URL to be "alien.json". It will always add `.json` as the extension. * If you do not desire this action then provide a URL. * * @method Phaser.Loader#json * @param {string} key - Unique asset key of the json file. * @param {string} [url] - URL of the JSON file. If undefined or `null` the url will be set to `<key>.json`, i.e. if `key` was "alien" then the URL will be "alien.json". * @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) { return this.addToFileList('json', key, url, undefined, overwrite, '.json'); }, /** * Adds a fragment shader file to the current load queue. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load. * * Retrieve the file via `Cache.getShader(key)`. * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "blur" * and no URL is given then the Loader will set the URL to be "blur.frag". It will always add `.frag` as the extension. * If you do not desire this action then provide a URL. * * @method Phaser.Loader#shader * @param {string} key - Unique asset key of the fragment file. * @param {string} [url] - URL of the fragment file. If undefined or `null` the url will be set to `<key>.frag`, i.e. if `key` was "blur" then the URL will be "blur.frag". * @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. */ shader: function (key, url, overwrite) { return this.addToFileList('shader', key, url, undefined, overwrite, '.frag'); }, /** * Adds an XML file to the current load queue. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load. * * Retrieve the file via `Cache.getXML(key)`. * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" * and no URL is given then the Loader will set the URL to be "alien.xml". It will always add `.xml` as the extension. * If you do not desire this action then provide a URL. * * @method Phaser.Loader#xml * @param {string} key - Unique asset key of the xml file. * @param {string} [url] - URL of the XML file. If undefined or `null` the url will be set to `<key>.xml`, i.e. if `key` was "alien" then the URL will be "alien.xml". * @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) { return this.addToFileList('xml', key, url, undefined, overwrite, '.xml'); }, /** * Adds a JavaScript file to the current load queue. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * The key must be a unique String. * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" * and no URL is given then the Loader will set the URL to be "alien.js". It will always add `.js` as the extension. * If you do not desire this action then provide a URL. * * Upon successful load the 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`. * * @method Phaser.Loader#script * @param {string} key - Unique asset key of the script file. * @param {string} [url] - URL of the JavaScript file. If undefined or `null` the url will be set to `<key>.js`, i.e. if `key` was "alien" then the URL will be "alien.js". * @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 Phaser Loader as the context. * @return {Phaser.Loader} This Loader instance. */ script: function (key, url, callback, callbackContext) { if (callback === undefined) { callback = false; } if (callback !== false && callbackContext === undefined) { callbackContext = this; } return this.addToFileList('script', key, url, { syncPoint: true, callback: callback, callbackContext: callbackContext }, false, '.js'); }, /** * Adds a binary file to the current load queue. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load. * * Retrieve the file via `Cache.getBinary(key)`. * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" * and no URL is given then the Loader will set the URL to be "alien.bin". It will always add `.bin` as the extension. * If you do not desire this action then provide a URL. * * It will be loaded via xhr with a responseType of "arraybuffer". You can specify an optional callback to process the file after load. * When the callback is called it will be passed 2 parameters: the key of the file and the file data. * * 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. * * @method Phaser.Loader#binary * @param {string} key - Unique asset key of the binary file. * @param {string} [url] - URL of the binary file. If undefined or `null` the url will be set to `<key>.bin`, i.e. if `key` was "alien" then the URL will be "alien.bin". * @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. * @return {Phaser.Loader} This Loader instance. */ binary: function (key, url, callback, callbackContext) { if (callback === undefined) { callback = false; } // Why is the default callback context the ..callback? if (callback !== false && callbackContext === undefined) { callbackContext = callback; } return this.addToFileList('binary', key, url, { callback: callback, callbackContext: callbackContext }, false, '.bin'); }, /** * Adds a Sprite Sheet to the current load queue. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * To clarify the terminology that Phaser uses: A Sprite Sheet is an image containing frames, usually of an animation, that are all equal * dimensions and often in sequence. For example if the frame size is 32x32 then every frame in the sprite sheet will be that size. * Sometimes (outside of Phaser) the term "sprite sheet" is used to refer to a texture atlas. * A Texture Atlas works by packing together images as best it can, using whatever frame sizes it likes, often with cropping and trimming * the frames in the process. Software such as Texture Packer, Flash CC or Shoebox all generate texture atlases, not sprite sheets. * If you've got an atlas then use `Loader.atlas` instead. * * The key must be a unique String. It is used to add the image to the Phaser.Cache upon successful load. * * Retrieve the file via `Cache.getImage(key)`. Sprite sheets, being image based, live in the same Cache as all other Images. * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * If the URL isn't specified the Loader will take the key and create a filename from that. For example if the key is "alien" * and no URL is given then the Loader will set the URL to be "alien.png". It will always add `.png` as the extension. * If you do not desire this action then provide a URL. * * @method Phaser.Loader#spritesheet * @param {string} key - Unique asset key of the sheet file. * @param {string} url - URL of the sprite sheet file. If undefined or `null` the url will be set to `<key>.png`, i.e. if `key` was "alien" then the URL will be "alien.png". * @param {number} frameWidth - Width in pixels of a single frame in the sprite sheet. * @param {number} frameHeight - Height in pixels of a single frame in the sprite sheet. * @param {number} [frameMax=-1] - How many frames in this sprite sheet. If not specified it will divide the whole image into frames. * @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. * @param {number} [skipFrames=0] - Skip a number of frames. Useful when there are multiple sprite sheets in one image. * @return {Phaser.Loader} This Loader instance. */ spritesheet: function (key, url, frameWidth, frameHeight, frameMax, margin, spacing, skipFrames) { if (frameMax === undefined) { frameMax = -1; } if (margin === undefined) { margin = 0; } if (spacing === undefined) { spacing = 0; } if (skipFrames === undefined) { skipFrames = 0; } return this.addToFileList('spritesheet', key, url, { frameWidth: frameWidth, frameHeight: frameHeight, frameMax: frameMax, margin: margin, spacing: spacing, skipFrames: skipFrames }, false, '.png'); }, /** * Adds an audio file to the current load queue. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load. * * Retrieve the file via `Cache.getSound(key)`. * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * Mobile warning: There are some mobile devices (certain iPad 2 and iPad Mini revisions) that cannot play 48000 Hz audio. * When they try to play the audio becomes extremely distorted and buzzes, eventually crashing the sound system. * The solution is to use a lower encoding rate such as 44100 Hz. * * @method Phaser.Loader#audio * @param {string} key - Unique asset key of the audio file. * @param {string|string[]|object[]} urls - Either a single string or an array of URIs or pairs of `{uri: .., type: ..}`. * If an array is specified then the first URI (or URI + mime pair) that is device-compatible will be selected. * For example: `"jump.mp3"`, `['jump.mp3', 'jump.ogg', 'jump.m4a']`, or `[{uri: "data:<opus_resource>", type: 'opus'}, 'fallback.mp3']`. * BLOB and DATA URIs can be used but only support automatic detection when used in the pair form; otherwise the format must be manually checked before adding the resource. * @param {boolean} [autoDecode=true] - When using Web Audio the audio files can either be decoded at load time or run-time. * Audio files can't be played until they are decoded and, if specified, this enables immediate decoding. Decoding is a non-blocking async process, however it consumes huge amounts of CPU time on mobiles especially. * @return {Phaser.Loader} This Loader instance. */ audio: function (key, urls, autoDecode) { if (this.game.sound.noAudio) { return this; } if (autoDecode === undefined) { autoDecode = true; } if (typeof urls === 'string') { urls = [urls]; } return this.addToFileList('audio', key, urls, { buffer: null, autoDecode: autoDecode }); }, /** * Adds an audio sprite file to the current load queue. * * The file is **not** loaded immediately after calling this method. The file is added to the queue ready to be loaded when the loader starts. * * The key must be a unique String. It is used to add the file to the Phaser.Cache upon successful load. * * Audio Sprites are a combination of audio files and a JSON configuration. * * The JSON follows the format of that created by https://github.com/tonistiigi/audiosprite * * Retrieve the file via `Cache.getSoundData(key)`. * * The URL can be relative or absolute. If the URL is relative the `Loader.baseURL` and `Loader.path` values will be prepended to it. * * @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. * @param {string} [jsonURL=null] - The URL of the audiosprite configuration JSON object. If you wish to pass the data directly set this parameter to null. * @param {string|object} [jsonData=null] - A JSON object or string containing the audiosprite configuration data. This is ignored if jsonURL is not null. * @param {boolean} [autoDecode=true] - When using Web Audio the audio files can either be decoded at load time or run-time. * Audio files can't be played until they are decoded and, if specified, this enables immediate decoding. Decoding is a non-blocking async process, however it consumes huge amounts of CPU time on mobiles especially. * @return {Phaser.Loader} This Loader instance. */ audioSprite: function (key, urls, jsonURL, jsonData, autoDecode) { if (this.game.sound.noAudio) { return this; } if (jsonURL === undefined) { jsonURL = null; } if (jsonData === undefined) { jsonData = null; } if (autoDecode === undefined) { autoDecode = true; } this.audio(key, urls, autoDecode); if (jsonURL) { this.json(key + '-audioatlas', jsonURL); } else if (jsonData) { if (typeof jsonData === 'string') { jsonData = JSON.parse(jsonData); } this.cache.addJSON(key + '-audioatlas', '', jsonData); } else { console.warn('Phaser.Loader.audiosprite - You must specify either a jsonURL or provide a jsonData object'); } return this