mirror of
https://github.com/wassname/phaser.git
synced 2026-07-17 11:31:30 +08:00
You can now load any binary file via the Loader: game.load.binary(key, url, callback) - the optional callback allows for post-load processing before entering the Cache.
This commit is contained in:
+65
-100
@@ -7,9 +7,8 @@
|
||||
/**
|
||||
* Phaser.Cache constructor.
|
||||
*
|
||||
* @class Phaser.Cache
|
||||
* @classdesc A game only has one instance of a Cache and it is used to store all externally loaded assets such
|
||||
* as images, sounds and data files as a result of Loader calls. Cache items use string based keys for look-up.
|
||||
* @class Phaser.Cache
|
||||
* @classdesc A game only has one instance of a Cache and it is used to store all externally loaded assets such as images, sounds and data files as a result of Loader calls. Cached items use string based keys for look-up.
|
||||
* @constructor
|
||||
* @param {Phaser.Game} game - A reference to the currently running game.
|
||||
*/
|
||||
@@ -57,10 +56,10 @@ Phaser.Cache = function (game) {
|
||||
this._tilemaps = {};
|
||||
|
||||
/**
|
||||
* @property {object} _tilesets - Tileset key-value container.
|
||||
* @property {object} _binary - Binary file key-value container.
|
||||
* @private
|
||||
*/
|
||||
this._tilesets = {};
|
||||
this._binary = {};
|
||||
|
||||
/**
|
||||
* @property {object} _bitmapDatas - BitmapData key-value container.
|
||||
@@ -93,6 +92,18 @@ Phaser.Cache.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a binary object in to the cache.
|
||||
* @method Phaser.Cache#addBinary
|
||||
* @param {string} key - Asset key for this binary data.
|
||||
* @param {object} binaryData - The binary object to be addded to the cache.
|
||||
*/
|
||||
addBinary: function (key, binaryData) {
|
||||
|
||||
this._binary[key] = binaryData;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a BitmapData object in to the cache.
|
||||
* @method Phaser.Cache#addBitmapData
|
||||
@@ -147,32 +158,6 @@ Phaser.Cache.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a new tile set in to the cache.
|
||||
*
|
||||
* @method Phaser.Cache#addTileset
|
||||
* @param {string} key - The unique key by which you will reference this object.
|
||||
* @param {string} url - URL of this tile set file.
|
||||
* @param {object} data - Extra tile set data.
|
||||
* @param {number} tileWidth - Width of each single tile in pixels.
|
||||
* @param {number} tileHeight - Height of each single tile in pixels.
|
||||
* @param {number} [tileMargin=0] - If the tiles have been drawn with a margin, specify the amount here.
|
||||
* @param {number} [tileSpacing=0] - If the tiles have been drawn with spacing between them, specify the amount here.
|
||||
* @param {number} [rows=-1] - How many tiles are placed horizontally in each row? If -1 it will calculate rows by dividing the image width by tileWidth.
|
||||
* @param {number} [columns=-1] - How many tiles are placed vertically in each column? If -1 it will calculate columns by dividing the image height by tileHeight.
|
||||
* @param {number} [total=-1] - The maximum number of tiles to extract from the image. If -1 it will extract `rows * columns` worth. You can also set a value lower than the actual number of tiles.
|
||||
*/
|
||||
addTileset: function (key, url, data, tileWidth, tileHeight, tileMargin, tileSpacing, rows, columns, total) {
|
||||
|
||||
this._tilesets[key] = { url: url, data: data, tileWidth: tileWidth, tileHeight: tileHeight, tileMargin: tileMargin, tileSpacing: tileSpacing, rows: rows, columns: columns, total: total };
|
||||
|
||||
PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data);
|
||||
PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]);
|
||||
|
||||
this._tilesets[key].tileData = Phaser.TilemapParser.tileset(this.game, key, tileWidth, tileHeight, tileMargin, tileSpacing, rows, columns, total);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a new tilemap.
|
||||
*
|
||||
@@ -411,8 +396,8 @@ Phaser.Cache.prototype = {
|
||||
* Get a canvas object from the cache by its key.
|
||||
*
|
||||
* @method Phaser.Cache#getCanvas
|
||||
* @param {string} key - Asset key of the canvas you want.
|
||||
* @return {object} The canvas you want.
|
||||
* @param {string} key - Asset key of the canvas to retrieve from the Cache.
|
||||
* @return {object} The canvas object.
|
||||
*/
|
||||
getCanvas: function (key) {
|
||||
|
||||
@@ -431,7 +416,7 @@ Phaser.Cache.prototype = {
|
||||
* Get a BitmapData object from the cache by its key.
|
||||
*
|
||||
* @method Phaser.Cache#getBitmapData
|
||||
* @param {string} key - Asset key of the BitmapData object you want.
|
||||
* @param {string} key - Asset key of the BitmapData object to retrieve from the Cache.
|
||||
* @return {Phaser.BitmapData} The requested BitmapData object if found, or null if not.
|
||||
*/
|
||||
getBitmapData: function (key) {
|
||||
@@ -451,7 +436,7 @@ Phaser.Cache.prototype = {
|
||||
* Checks if an image key exists.
|
||||
*
|
||||
* @method Phaser.Cache#checkImageKey
|
||||
* @param {string} key - Asset key of the image you want.
|
||||
* @param {string} key - Asset key of the image to check is in the Cache.
|
||||
* @return {boolean} True if the key exists, otherwise false.
|
||||
*/
|
||||
checkImageKey: function (key) {
|
||||
@@ -469,8 +454,8 @@ Phaser.Cache.prototype = {
|
||||
* Get image data by key.
|
||||
*
|
||||
* @method Phaser.Cache#getImage
|
||||
* @param {string} key - Asset key of the image you want.
|
||||
* @return {object} The image data you want.
|
||||
* @param {string} key - Asset key of the image to retrieve from the Cache.
|
||||
* @return {object} The image data.
|
||||
*/
|
||||
getImage: function (key) {
|
||||
|
||||
@@ -485,52 +470,12 @@ Phaser.Cache.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Get tile set image data by key.
|
||||
*
|
||||
* @method Phaser.Cache#getTileSetImage
|
||||
* @param {string} key - Asset key of the image you want.
|
||||
* @return {object} The image data you want.
|
||||
*/
|
||||
getTilesetImage: function (key) {
|
||||
|
||||
if (this._tilesets[key])
|
||||
{
|
||||
return this._tilesets[key].data;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.warn('Phaser.Cache.getTilesetImage: Invalid key: "' + key + '"');
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Get tile set image data by key.
|
||||
*
|
||||
* @method Phaser.Cache#getTileset
|
||||
* @param {string} key - Asset key of the image you want.
|
||||
* @return {Phaser.Tileset} The tileset data. The tileset image is in the data property, the tile data in tileData.
|
||||
*/
|
||||
getTileset: function (key) {
|
||||
|
||||
if (this._tilesets[key])
|
||||
{
|
||||
return this._tilesets[key].tileData;
|
||||
}
|
||||
else
|
||||
{
|
||||
console.warn('Phaser.Cache.getTileset: Invalid key: "' + key + '"');
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Get tilemap data by key.
|
||||
*
|
||||
* @method Phaser.Cache#getTilemap
|
||||
* @param {string} key - Asset key of the tilemap you want.
|
||||
* @return {Object} The tilemap data. The tileset image is in the data property, the map data in mapData.
|
||||
* @param {string} key - Asset key of the tilemap data to retrieve from the Cache.
|
||||
* @return {Object} The raw tilemap data in CSV or JSON format.
|
||||
*/
|
||||
getTilemapData: function (key) {
|
||||
|
||||
@@ -549,8 +494,8 @@ Phaser.Cache.prototype = {
|
||||
* Get frame data by key.
|
||||
*
|
||||
* @method Phaser.Cache#getFrameData
|
||||
* @param {string} key - Asset key of the frame data you want.
|
||||
* @return {Phaser.FrameData} The frame data you want.
|
||||
* @param {string} key - Asset key of the frame data to retrieve from the Cache.
|
||||
* @return {Phaser.FrameData} The frame data.
|
||||
*/
|
||||
getFrameData: function (key) {
|
||||
|
||||
@@ -566,8 +511,8 @@ Phaser.Cache.prototype = {
|
||||
* Get a single frame out of a frameData set by key.
|
||||
*
|
||||
* @method Phaser.Cache#getFrameByIndex
|
||||
* @param {string} key - Asset key of the frame data you want.
|
||||
* @return {Phaser.Frame} The frame data you want.
|
||||
* @param {string} key - Asset key of the frame data to retrieve from the Cache.
|
||||
* @return {Phaser.Frame} The frame object.
|
||||
*/
|
||||
getFrameByIndex: function (key, frame) {
|
||||
|
||||
@@ -583,8 +528,8 @@ Phaser.Cache.prototype = {
|
||||
* Get a single frame out of a frameData set by key.
|
||||
*
|
||||
* @method Phaser.Cache#getFrameByName
|
||||
* @param {string} key - Asset key of the frame data you want.
|
||||
* @return {Phaser.Frame} The frame data you want.
|
||||
* @param {string} key - Asset key of the frame data to retrieve from the Cache.
|
||||
* @return {Phaser.Frame} The frame object.
|
||||
*/
|
||||
getFrameByName: function (key, frame) {
|
||||
|
||||
@@ -600,8 +545,8 @@ Phaser.Cache.prototype = {
|
||||
* Get a single frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.
|
||||
*
|
||||
* @method Phaser.Cache#getFrame
|
||||
* @param {string} key - Asset key of the frame data you want.
|
||||
* @return {Phaser.Frame} The frame data you want.
|
||||
* @param {string} key - Asset key of the frame data to retrieve from the Cache.
|
||||
* @return {Phaser.Frame} The frame data.
|
||||
*/
|
||||
getFrame: function (key) {
|
||||
|
||||
@@ -614,11 +559,11 @@ Phaser.Cache.prototype = {
|
||||
},
|
||||
|
||||
/**
|
||||
* Get a single frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.
|
||||
* Get a single texture frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.
|
||||
*
|
||||
* @method Phaser.Cache#getTextureFrame
|
||||
* @param {string} key - Asset key of the frame data you want.
|
||||
* @return {Phaser.Frame} The frame data you want.
|
||||
* @param {string} key - Asset key of the frame to retrieve from the Cache.
|
||||
* @return {Phaser.Frame} The frame data.
|
||||
*/
|
||||
getTextureFrame: function (key) {
|
||||
|
||||
@@ -634,8 +579,8 @@ Phaser.Cache.prototype = {
|
||||
* Get a RenderTexture by key.
|
||||
*
|
||||
* @method Phaser.Cache#getTexture
|
||||
* @param {string} key - Asset key of the RenderTexture you want.
|
||||
* @return {Phaser.RenderTexture} The RenderTexture you want.
|
||||
* @param {string} key - Asset key of the RenderTexture to retrieve from the Cache.
|
||||
* @return {Phaser.RenderTexture} The RenderTexture object.
|
||||
*/
|
||||
getTexture: function (key) {
|
||||
|
||||
@@ -654,8 +599,8 @@ Phaser.Cache.prototype = {
|
||||
* Get sound by key.
|
||||
*
|
||||
* @method Phaser.Cache#getSound
|
||||
* @param {string} key - Asset key of the sound you want.
|
||||
* @return {Phaser.Sound} The sound you want.
|
||||
* @param {string} key - Asset key of the sound to retrieve from the Cache.
|
||||
* @return {Phaser.Sound} The sound object.
|
||||
*/
|
||||
getSound: function (key) {
|
||||
|
||||
@@ -674,8 +619,8 @@ Phaser.Cache.prototype = {
|
||||
* Get sound data by key.
|
||||
*
|
||||
* @method Phaser.Cache#getSoundData
|
||||
* @param {string} key - Asset key of the sound you want.
|
||||
* @return {object} The sound data you want.
|
||||
* @param {string} key - Asset key of the sound to retrieve from the Cache.
|
||||
* @return {object} The sound data.
|
||||
*/
|
||||
getSoundData: function (key) {
|
||||
|
||||
@@ -694,7 +639,7 @@ Phaser.Cache.prototype = {
|
||||
* Check if the given sound has finished decoding.
|
||||
*
|
||||
* @method Phaser.Cache#isSoundDecoded
|
||||
* @param {string} key - Asset key of the sound you want.
|
||||
* @param {string} key - Asset key of the sound in the Cache.
|
||||
* @return {boolean} The decoded state of the Sound object.
|
||||
*/
|
||||
isSoundDecoded: function (key) {
|
||||
@@ -710,7 +655,7 @@ Phaser.Cache.prototype = {
|
||||
* Check if the given sound is ready for playback. A sound is considered ready when it has finished decoding and the device is no longer touch locked.
|
||||
*
|
||||
* @method Phaser.Cache#isSoundReady
|
||||
* @param {string} key - Asset key of the sound you want.
|
||||
* @param {string} key - Asset key of the sound in the Cache.
|
||||
* @return {boolean} True if the sound is decoded and the device is not touch locked.
|
||||
*/
|
||||
isSoundReady: function (key) {
|
||||
@@ -741,8 +686,8 @@ Phaser.Cache.prototype = {
|
||||
* Get text data by key.
|
||||
*
|
||||
* @method Phaser.Cache#getText
|
||||
* @param {string} key - Asset key of the text data you want.
|
||||
* @return {object} The text data you want.
|
||||
* @param {string} key - Asset key of the text data to retrieve from the Cache.
|
||||
* @return {object} The text data.
|
||||
*/
|
||||
getText: function (key) {
|
||||
|
||||
@@ -757,6 +702,26 @@ Phaser.Cache.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Get binary data by key.
|
||||
*
|
||||
* @method Phaser.Cache#getBinary
|
||||
* @param {string} key - Asset key of the binary data object to retrieve from the Cache.
|
||||
* @return {object} The binary data object.
|
||||
*/
|
||||
getBinary: function (key) {
|
||||
|
||||
if (this._binary[key])
|
||||
{
|
||||
return this._binary[key];
|
||||
}
|
||||
else
|
||||
{
|
||||
console.warn('Phaser.Cache.getBinary: Invalid key: "' + key + '"');
|
||||
}
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the cache keys from a given array of objects.
|
||||
* Normally you don't call this directly but instead use getImageKeys, getSoundKeys, etc.
|
||||
|
||||
+40
-3
@@ -363,6 +363,29 @@ Phaser.Loader.prototype = {
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* When the callback is called it will be passed 2 parameters: the key of the file and the file data.
|
||||
* WARNING: If you specify a callback, the file data will be set to whatever your callback returns. So 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.
|
||||
* @param {function} [callback] - Optional callback that will be passed the file after loading, so you can perform additional processing on it.
|
||||
* @param {function} [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 (typeof callback === 'undefined') { callback = false; }
|
||||
if (callback !== false && typeof callbackContext === 'undefined') { callbackContext = callback; }
|
||||
|
||||
this.addToFileList('binary', key, url, { callback: callback, callbackContext: callbackContext });
|
||||
|
||||
return this;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Add a new sprite sheet to the loader.
|
||||
*
|
||||
@@ -854,6 +877,7 @@ Phaser.Loader.prototype = {
|
||||
break;
|
||||
|
||||
case 'text':
|
||||
case 'script':
|
||||
this._xhr.open("GET", this.baseURL + file.url, true);
|
||||
this._xhr.responseType = "text";
|
||||
this._xhr.onload = function () {
|
||||
@@ -865,10 +889,9 @@ Phaser.Loader.prototype = {
|
||||
this._xhr.send();
|
||||
break;
|
||||
|
||||
case 'script':
|
||||
|
||||
case 'binary':
|
||||
this._xhr.open("GET", this.baseURL + file.url, true);
|
||||
this._xhr.responseType = "text";
|
||||
this._xhr.responseType = "arraybuffer";
|
||||
this._xhr.onload = function () {
|
||||
return _this.fileComplete(_this._fileIndex);
|
||||
};
|
||||
@@ -1070,6 +1093,20 @@ Phaser.Loader.prototype = {
|
||||
file.data.text = this._xhr.responseText;
|
||||
document.head.appendChild(file.data);
|
||||
break;
|
||||
|
||||
case 'binary':
|
||||
if (file.callback)
|
||||
{
|
||||
file.data = file.callback.call(file.callbackContext, file.key, this._xhr.response);
|
||||
}
|
||||
else
|
||||
{
|
||||
file.data = this._xhr.response;
|
||||
}
|
||||
|
||||
this.game.cache.addBinary(file.key, file.data);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (loadNext)
|
||||
|
||||
Reference in New Issue
Block a user