Loader can now natively load XML files via load.xml. Once the XML file has loaded it is parsed via either DOMParser or ActiveXObject and then added to the Cache, where it can be retrieved via cache.getXML(key).

Cache now has support for XML files stored in their own container. You can add them with `cache.addXML` (typically this is done from the Loader automatically for you) and get them with `cache.getXML(key)`. There is also `cache.checkXMLKey(key)`, `cache.checkKeys` and `cache.removeXML(key)`.
This commit is contained in:
photonstorm 2014-09-19 00:22:00 +01:00
parent 5a58d6be38
commit 5980a3bdc6
3 changed files with 141 additions and 15 deletions

View file

@ -78,6 +78,8 @@ Version 2.1.2 - "Whitebridge" - in development
* StateManager.unlink will null all State-level Phaser properties, such as `game`, `add`, etc. Useful if you never need to return to the State again.
* Cache.removeImage has a new parameter: `removeFromPixi` which is `true` by default. It will remove the image from the Pixi BaseTextureCache as well as from the Phaser Cache. Set to false if you don't want the Pixi cache touched.
* Group.ignoreDestroy boolean will bail out early from any call to `Group.destroy`. Handy if you need to create a global Group that persists across States.
* Loader can now natively load XML files via `load.xml`. Once the XML file has loaded it is parsed via either DOMParser or ActiveXObject and then added to the Cache, where it can be retrieved via `cache.getXML(key)`.
* Cache now has support for XML files stored in their own container. You can add them with `cache.addXML` (typically this is done from the Loader automatically for you) and get them with `cache.getXML(key)`. There is also `cache.checkXMLKey(key)`, `cache.checkKeys` and `cache.removeXML(key)`.
### Updates

View file

@ -55,6 +55,12 @@ Phaser.Cache = function (game) {
*/
this._json = {};
/**
* @property {object} _xml - XML key-value container.
* @private
*/
this._xml = {};
/**
* @property {object} _physics - Physics data key-value container.
* @private
@ -109,6 +115,7 @@ Phaser.Cache = function (game) {
this._cacheMap[Phaser.Cache.BITMAPDATA] = this._bitmapDatas;
this._cacheMap[Phaser.Cache.BITMAPFONT] = this._bitmapFont;
this._cacheMap[Phaser.Cache.JSON] = this._json;
this._cacheMap[Phaser.Cache.XML] = this._xml;
};
@ -178,6 +185,12 @@ Phaser.Cache.BITMAPFONT = 10;
*/
Phaser.Cache.JSON = 11;
/**
* @constant
* @type {number}
*/
Phaser.Cache.XML = 12;
Phaser.Cache.prototype = {
/**
@ -408,9 +421,9 @@ Phaser.Cache.prototype = {
* Add a new json object into the cache.
*
* @method Phaser.Cache#addJSON
* @param {string} key - Asset key for the text data.
* @param {string} url - URL of this text data file.
* @param {object} data - Extra text data.
* @param {string} key - Asset key for the json data.
* @param {string} url - URL of this json data file.
* @param {object} data - Extra json data.
*/
addJSON: function (key, url, data) {
@ -418,6 +431,20 @@ Phaser.Cache.prototype = {
},
/**
* Add a new xml object into the cache.
*
* @method Phaser.Cache#addXML
* @param {string} key - Asset key for the xml file.
* @param {string} url - URL of this xml file.
* @param {object} data - Extra text data.
*/
addXML: function (key, url, data) {
this._xml[key] = { url: url, data: data };
},
/**
* Adds an Image file into the Cache. The file must have already been loaded, typically via Phaser.Loader.
*
@ -679,7 +706,7 @@ Phaser.Cache.prototype = {
* Checks if the given key exists in the Canvas Cache.
*
* @method Phaser.Cache#checkCanvasKey
* @param {string} key - Asset key of the image to check is in the Cache.
* @param {string} key - Asset key of the canvas to check is in the Cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkCanvasKey: function (key) {
@ -718,7 +745,7 @@ Phaser.Cache.prototype = {
* Checks if the given key exists in the Sound Cache.
*
* @method Phaser.Cache#checkSoundKey
* @param {string} key - Asset key of the image to check is in the Cache.
* @param {string} key - Asset key of the sound file to check is in the Cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkSoundKey: function (key) {
@ -731,7 +758,7 @@ Phaser.Cache.prototype = {
* Checks if the given key exists in the Text Cache.
*
* @method Phaser.Cache#checkTextKey
* @param {string} key - Asset key of the image to check is in the Cache.
* @param {string} key - Asset key of the text file to check is in the Cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkTextKey: function (key) {
@ -744,7 +771,7 @@ Phaser.Cache.prototype = {
* Checks if the given key exists in the Physics Cache.
*
* @method Phaser.Cache#checkPhysicsKey
* @param {string} key - Asset key of the image to check is in the Cache.
* @param {string} key - Asset key of the physics data file to check is in the Cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkPhysicsKey: function (key) {
@ -757,7 +784,7 @@ Phaser.Cache.prototype = {
* Checks if the given key exists in the Tilemap Cache.
*
* @method Phaser.Cache#checkTilemapKey
* @param {string} key - Asset key of the image to check is in the Cache.
* @param {string} key - Asset key of the Tilemap to check is in the Cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkTilemapKey: function (key) {
@ -770,7 +797,7 @@ Phaser.Cache.prototype = {
* Checks if the given key exists in the Binary Cache.
*
* @method Phaser.Cache#checkBinaryKey
* @param {string} key - Asset key of the image to check is in the Cache.
* @param {string} key - Asset key of the binary file to check is in the Cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkBinaryKey: function (key) {
@ -783,7 +810,7 @@ Phaser.Cache.prototype = {
* Checks if the given key exists in the BitmapData Cache.
*
* @method Phaser.Cache#checkBitmapDataKey
* @param {string} key - Asset key of the image to check is in the Cache.
* @param {string} key - Asset key of the BitmapData to check is in the Cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkBitmapDataKey: function (key) {
@ -796,7 +823,7 @@ Phaser.Cache.prototype = {
* Checks if the given key exists in the BitmapFont Cache.
*
* @method Phaser.Cache#checkBitmapFontKey
* @param {string} key - Asset key of the image to check is in the Cache.
* @param {string} key - Asset key of the BitmapFont to check is in the Cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkBitmapFontKey: function (key) {
@ -809,7 +836,7 @@ Phaser.Cache.prototype = {
* Checks if the given key exists in the JSON Cache.
*
* @method Phaser.Cache#checkJSONKey
* @param {string} key - Asset key of the image to check is in the Cache.
* @param {string} key - Asset key of the JSON file to check is in the Cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkJSONKey: function (key) {
@ -818,6 +845,19 @@ Phaser.Cache.prototype = {
},
/**
* Checks if the given key exists in the XML Cache.
*
* @method Phaser.Cache#checkXMLKey
* @param {string} key - Asset key of the XML file to check is in the Cache.
* @return {boolean} True if the key exists, otherwise false.
*/
checkXMLKey: function (key) {
return this.checkKey(Phaser.Cache.XML, key);
},
/**
* Get image data by key.
*
@ -1107,6 +1147,26 @@ Phaser.Cache.prototype = {
},
/**
* Get a XML object by key from the cache.
*
* @method Phaser.Cache#getXML
* @param {string} key - Asset key of the XML object to retrieve from the Cache.
* @return {object} The XML object.
*/
getXML: function (key) {
if (this._xml[key])
{
return this._xml[key].data;
}
else
{
console.warn('Phaser.Cache.getXML: Invalid key: "' + key + '"');
}
},
/**
* Get binary data by key.
*
@ -1183,6 +1243,10 @@ Phaser.Cache.prototype = {
case Phaser.Cache.JSON:
array = this._json;
break;
case Phaser.Cache.XML:
array = this._xml;
break;
}
if (!array)
@ -1264,6 +1328,16 @@ Phaser.Cache.prototype = {
delete this._json[key];
},
/**
* Removes a xml object from the cache.
*
* @method Phaser.Cache#removeXML
* @param {string} key - Key of the asset you want to remove.
*/
removeXML: function (key) {
delete this._xml[key];
},
/**
* Removes a physics data file from the cache.
*
@ -1349,6 +1423,11 @@ Phaser.Cache.prototype = {
delete this._json[item];
}
for (var item in this._xml)
{
delete this._xml[item];
}
for (var item in this._textures)
{
delete this._textures[item];

View file

@ -499,6 +499,32 @@ Phaser.Loader.prototype = {
},
/**
* 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; }
if (overwrite)
{
this.replaceInFileList('xml', key, url);
}
else
{
this.addToFileList('xml', key, url);
}
return this;
},
/**
* Add a JavaScript file to the Loader. Once loaded the JavaScript file will be automatically turned into a script tag (and executed), so be careful what you load!
* You can also specify a callback. This will be executed as soon as the script tag has been created.
@ -1042,6 +1068,10 @@ Phaser.Loader.prototype = {
this.json(file.key, file.url, file.overwrite);
break;
case "xml":
this.xml(file.key, file.url, file.overwrite);
break;
case "script":
this.script(file.key, file.url, file.callback, pack.callbackContext);
break;
@ -1153,7 +1183,7 @@ Phaser.Loader.prototype = {
var file = this._fileList[this._fileIndex];
var _this = this;
this.onFileStart.dispatch(this.progress, file.key);
this.onFileStart.dispatch(this.progress, file.key, file.url);
// Image or Data?
switch (file.type)
@ -1259,6 +1289,11 @@ Phaser.Loader.prototype = {
break;
case 'xml':
this.xhrLoad(this._fileIndex, this.baseURL + file.url, 'text', 'xmlLoadComplete', 'dataLoadError');
break;
case 'tilemap':
if (file.format === Phaser.Tilemap.TILED_JSON)
@ -1609,6 +1644,12 @@ Phaser.Loader.prototype = {
*/
xmlLoadComplete: function (index) {
if (this._xhr.responseType !== '' && this._xhr.responseType !== 'text')
{
console.warn('Invalid XML Response Type', this._fileList[index]);
console.warn(this._xhr);
}
var data = this._xhr.responseText;
var xml;
@ -1639,14 +1680,18 @@ Phaser.Loader.prototype = {
var file = this._fileList[index];
file.loaded = true;
if (file.type == 'bitmapfont')
if (file.type === 'bitmapfont')
{
this.game.cache.addBitmapFont(file.key, file.url, file.data, xml, file.xSpacing, file.ySpacing);
}
else if (file.type == 'textureatlas')
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);
}
this.nextFile(index, true);