Phaser.Loader = function (game){ this.game = game; this._fileList = [] ; this._fileIndex = 0; this._progressChunk = 0; this._xhr = new XMLHttpRequest(); this._ajax = null ; this.isLoading = false ; this.hasLoaded = false ; this.progress = 0; this.progressFloat = 0; this.preloadSprite = null ; this.crossOrigin = false ; _AN_Write_baseurl('baseURL', this, false , ''); this.onLoadStart = new Phaser.Signal(); this.onFileStart = new Phaser.Signal(); this.onFileComplete = new Phaser.Signal(); this.onFileError = new Phaser.Signal(); this.onLoadComplete = new Phaser.Signal(); } ; Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY = 0; Phaser.Loader.TEXTURE_ATLAS_JSON_HASH = 1; Phaser.Loader.TEXTURE_ATLAS_XML_STARLING = 2; Phaser.Loader.PHYSICS_LIME_CORONA_JSON = 3; Phaser.Loader.PHYSICS_PHASER_JSON = 4; Phaser.Loader.prototype = { setPreloadSprite: function (sprite, direction){ direction = direction || 0; this.preloadSprite = { sprite: sprite, direction: direction, width: sprite.width, height: sprite.height, rect: null } ; if (direction === 0) { this.preloadSprite.rect = new Phaser.Rectangle(0, 0, 1, sprite.height); } else { this.preloadSprite.rect = new Phaser.Rectangle(0, 0, sprite.width, 1); } sprite.crop(this.preloadSprite.rect); sprite.visible = true ; } , checkKeyExists: function (type, key){ if (_AN_Read_length('length', this._fileList) > 0) { for (var i = 0; i < _AN_Read_length('length', this._fileList); i++ ){ if (this._fileList[i].type === type && this._fileList[i].key === key) { return true ; } } } return false ; } , getAssetIndex: function (type, key){ if (_AN_Read_length('length', this._fileList) > 0) { for (var i = 0; i < _AN_Read_length('length', this._fileList); i++ ){ if (this._fileList[i].type === type && this._fileList[i].key === key) { return i; } } } return -1; } , getAsset: function (type, key){ if (_AN_Read_length('length', this._fileList) > 0) { for (var i = 0; i < _AN_Read_length('length', this._fileList); i++ ){ if (this._fileList[i].type === type && this._fileList[i].key === key) { return { index: i, file: this._fileList[i]} ; } } } return false ; } , reset: function (){ this.preloadSprite = null ; this.isLoading = false ; this._fileList.length = 0; this._fileIndex = 0; } , addToFileList: function (type, key, url, properties){ var entry = { type: type, key: key, url: url, data: null , error: false , loaded: false } ; if (typeof properties !== "undefined") { for (var prop in properties){ entry[prop] = properties[prop]; } } if (this.checkKeyExists(type, key) === false ) { this._fileList.push(entry); } } , replaceInFileList: function (type, key, url, properties){ var entry = { type: type, key: key, url: url, data: null , error: false , loaded: false } ; if (typeof properties !== "undefined") { for (var prop in properties){ entry[prop] = properties[prop]; } } var index = this.getAssetIndex(type, key); if (index === -1) { this._fileList.push(entry); } else { this._fileList[index] = entry; } } , image: function (key, url, overwrite){ if (typeof overwrite === "undefined") { overwrite = false ; } if (overwrite) { this.replaceInFileList('image', key, url); } else { this.addToFileList('image', key, url); } return this; } , text: function (key, url, overwrite){ if (typeof overwrite === "undefined") { overwrite = false ; } if (overwrite) { this.replaceInFileList('text', key, url); } else { this.addToFileList('text', key, url); } return this; } , json: function (key, url, overwrite){ if (typeof overwrite === "undefined") { overwrite = false ; } if (overwrite) { this.replaceInFileList('json', key, url); } else { this.addToFileList('json', key, url); } return this; } , script: function (key, url, callback, callbackContext){ if (typeof callback === 'undefined') { callback = false ; } if (callback !== false && typeof callbackContext === 'undefined') { callbackContext = callback; } this.addToFileList('script', key, url, { callback: callback, callbackContext: callbackContext} ); return this; } , 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; } , spritesheet: function (key, url, frameWidth, frameHeight, frameMax, margin, spacing){ if (typeof frameMax === "undefined") { frameMax = -1; } if (typeof margin === "undefined") { margin = 0; } if (typeof spacing === "undefined") { spacing = 0; } this.addToFileList('spritesheet', key, url, { frameWidth: frameWidth, frameHeight: frameHeight, frameMax: frameMax, margin: margin, spacing: spacing} ); return this; } , audio: function (key, urls, autoDecode){ if (typeof autoDecode === "undefined") { autoDecode = true ; } this.addToFileList('audio', key, urls, { buffer: null , autoDecode: autoDecode} ); return this; } , tilemap: function (key, mapDataURL, mapData, format){ if (typeof mapDataURL === "undefined") { mapDataURL = null ; } if (typeof mapData === "undefined") { mapData = null ; } if (typeof format === "undefined") { format = Phaser.Tilemap.CSV; } if (mapDataURL == null && mapData == null ) { console.warn('Phaser.Loader.tilemap - Both mapDataURL and mapData are null. One must be set.'); return this; } if (mapData) { switch (format){ case Phaser.Tilemap.CSV: break ; case Phaser.Tilemap.TILED_JSON: if (typeof mapData === 'string') { mapData = JSON.parse(mapData); } break ; } this.game.cache.addTilemap(key, null , mapData, format); } else { this.addToFileList('tilemap', key, mapDataURL, { format: format} ); } return this; } , physics: function (key, dataURL, jsonData, format){ if (typeof dataURL === "undefined") { dataURL = null ; } if (typeof jsonData === "undefined") { jsonData = null ; } if (typeof format === "undefined") { format = Phaser.Physics.LIME_CORONA_JSON; } if (dataURL == null && jsonData == null ) { console.warn('Phaser.Loader.physics - Both dataURL and jsonData are null. One must be set.'); return this; } if (jsonData) { if (typeof jsonData === 'string') { jsonData = JSON.parse(jsonData); } this.game.cache.addPhysicsData(key, null , jsonData, format); } else { this.addToFileList('physics', key, dataURL, { format: format} ); } return this; } , bitmapFont: function (key, textureURL, xmlURL, xmlData, xSpacing, ySpacing){ if (typeof xmlURL === "undefined") { xmlURL = null ; } if (typeof xmlData === "undefined") { xmlData = null ; } if (typeof xSpacing === "undefined") { xSpacing = 0; } if (typeof ySpacing === "undefined") { ySpacing = 0; } if (xmlURL) { this.addToFileList('bitmapfont', key, textureURL, { xmlURL: xmlURL, xSpacing: xSpacing, ySpacing: ySpacing} ); } else { if (typeof xmlData === 'string') { var xml; try { if (window.DOMParser) { var domparser = new DOMParser(); xml = domparser.parseFromString(xmlData, "text/xml"); } else { xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = 'false'; xml.loadXML(xmlData); } } catch (e) { xml = undefined; } if (!xml || !xml.documentElement || _AN_Read_length('length', _AN_Call_getelementsbytagname('getElementsByTagName', xml, "parsererror"))) { throw new Error("Phaser.Loader. Invalid Bitmap Font XML given") } else { this.addToFileList('bitmapfont', key, textureURL, { xmlURL: null , xmlData: xml, xSpacing: xSpacing, ySpacing: ySpacing} ); } } } return this; } , atlasJSONArray: function (key, textureURL, atlasURL, atlasData){ return this.atlas(key, textureURL, atlasURL, atlasData, Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY); } , atlasJSONHash: function (key, textureURL, atlasURL, atlasData){ return this.atlas(key, textureURL, atlasURL, atlasData, Phaser.Loader.TEXTURE_ATLAS_JSON_HASH); } , atlasXML: function (key, textureURL, atlasURL, atlasData){ return this.atlas(key, textureURL, atlasURL, atlasData, Phaser.Loader.TEXTURE_ATLAS_XML_STARLING); } , 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; } if (atlasURL) { this.addToFileList('textureatlas', key, textureURL, { atlasURL: atlasURL, format: format} ); } else { switch (format){ case Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY: if (typeof atlasData === 'string') { atlasData = JSON.parse(atlasData); } break ; case Phaser.Loader.TEXTURE_ATLAS_XML_STARLING: if (typeof atlasData === 'string') { var xml; try { if (window.DOMParser) { var domparser = new DOMParser(); xml = domparser.parseFromString(atlasData, "text/xml"); } else { xml = new ActiveXObject("Microsoft.XMLDOM"); xml.async = 'false'; xml.loadXML(atlasData); } } catch (e) { xml = undefined; } if (!xml || !xml.documentElement || _AN_Read_length('length', _AN_Call_getelementsbytagname('getElementsByTagName', xml, "parsererror"))) { throw new Error("Phaser.Loader. Invalid Texture Atlas XML given") } else { atlasData = xml; } } break ; } this.addToFileList('textureatlas', key, textureURL, { atlasURL: null , atlasData: atlasData, format: format} ); } return this; } , removeFile: function (type, key){ var file = this.getAsset(type, key); if (file !== false ) { this._fileList.splice(file.index, 1); } } , removeAll: function (){ this._fileList.length = 0; } , start: function (){ if (this.isLoading) { return ; } this.progress = 0; this.progressFloat = 0; this.hasLoaded = false ; this.isLoading = true ; this.onLoadStart.dispatch(_AN_Read_length('length', this._fileList)); if (_AN_Read_length('length', this._fileList) > 0) { this._fileIndex = 0; this._progressChunk = 100 / _AN_Read_length('length', this._fileList); this.loadFile(); } else { this.progress = 100; this.progressFloat = 100; this.hasLoaded = true ; this.onLoadComplete.dispatch(); } } , loadFile: function (){ if (!this._fileList[this._fileIndex]) { console.warn('Phaser.Loader loadFile invalid index ' + this._fileIndex); return ; } var file = this._fileList[this._fileIndex]; var _this = this; this.onFileStart.dispatch(this.progress, file.key); switch (file.type){ case 'image': case 'spritesheet': case 'textureatlas': case 'bitmapfont': file.data = new Image(); file.data.name = file.key; file.data.onload = function (){ return _this.fileComplete(_this._fileIndex); } ; file.data.onerror = function (){ return _this.fileError(_this._fileIndex); } ; if (this.crossOrigin) { file.data.crossOrigin = this.crossOrigin; } _AN_Write_src('src', file.data, false , _AN_Read_baseurl('baseURL', this) + _AN_Read_url('url', file)); break ; case 'audio': _AN_Write_url('url', file, false , this.getAudioURL(_AN_Read_url('url', file))); if (_AN_Read_url('url', file) !== null ) { if (this.game.sound.usingWebAudio) { _AN_Call_open('open', this._xhr, "GET", _AN_Read_baseurl("baseURL", this) + _AN_Read_url("url", file), true ); this._xhr.responseType = "arraybuffer"; this._xhr.onload = function (){ return _this.fileComplete(_this._fileIndex); } ; this._xhr.onerror = function (){ return _this.fileError(_this._fileIndex); } ; _AN_Call_send("send", this._xhr); } else if (this.game.sound.usingAudioTag) { if (this.game.sound.touchLocked) { file.data = new Audio(); file.data.name = file.key; file.data.preload = 'auto'; _AN_Write_src('src', file.data, false , _AN_Read_baseurl('baseURL', this) + _AN_Read_url('url', file)); this.fileComplete(this._fileIndex); } else { file.data = new Audio(); file.data.name = file.key; file.data.onerror = function (){ return _this.fileError(_this._fileIndex); } ; file.data.preload = 'auto'; _AN_Write_src('src', file.data, false , _AN_Read_baseurl('baseURL', this) + _AN_Read_url('url', file)); file.data.addEventListener('canplaythrough', Phaser.GAMES[this.game.id].load.fileComplete(this._fileIndex), false ); _AN_Call_load('load', file.data); } } } else { this.fileError(this._fileIndex); } break ; case 'json': if (window.XDomainRequest) { this._ajax = new window.XDomainRequest(); this._ajax.timeout = 3000; this._ajax.onerror = function (){ return _this.dataLoadError(_this._fileIndex); } ; this._ajax.ontimeout = function (){ return _this.dataLoadError(_this._fileIndex); } ; this._ajax.onprogress = function (){ } ; this._ajax.onload = function (){ return _this.jsonLoadComplete(_this._fileIndex); } ; _AN_Call_open('open', this._ajax, 'GET', _AN_Read_baseurl('baseURL', this) + _AN_Read_url('url', file), true ); _AN_Call_send('send', this._ajax); } else { _AN_Call_open('open', this._xhr, "GET", _AN_Read_baseurl("baseURL", this) + _AN_Read_url("url", file), true ); this._xhr.responseType = "text"; this._xhr.onload = function (){ return _this.jsonLoadComplete(_this._fileIndex); } ; this._xhr.onerror = function (){ return _this.dataLoadError(_this._fileIndex); } ; _AN_Call_send("send", this._xhr); } break ; case 'tilemap': _AN_Call_open('open', this._xhr, "GET", _AN_Read_baseurl("baseURL", this) + _AN_Read_url("url", file), true ); this._xhr.responseType = "text"; if (file.format === Phaser.Tilemap.TILED_JSON) { this._xhr.onload = function (){ return _this.jsonLoadComplete(_this._fileIndex); } ; } else if (file.format === Phaser.Tilemap.CSV) { this._xhr.onload = function (){ return _this.csvLoadComplete(_this._fileIndex); } ; } else { throw new Error("Phaser.Loader. Invalid Tilemap format: " + file.format) } this._xhr.onerror = function (){ return _this.dataLoadError(_this._fileIndex); } ; _AN_Call_send("send", this._xhr); break ; case 'text': case 'script': case 'physics': _AN_Call_open('open', this._xhr, "GET", _AN_Read_baseurl("baseURL", this) + _AN_Read_url("url", file), true ); this._xhr.responseType = "text"; this._xhr.onload = function (){ return _this.fileComplete(_this._fileIndex); } ; this._xhr.onerror = function (){ return _this.fileError(_this._fileIndex); } ; _AN_Call_send("send", this._xhr); break ; case 'binary': _AN_Call_open('open', this._xhr, "GET", _AN_Read_baseurl("baseURL", this) + _AN_Read_url("url", file), true ); this._xhr.responseType = "arraybuffer"; this._xhr.onload = function (){ return _this.fileComplete(_this._fileIndex); } ; this._xhr.onerror = function (){ return _this.fileError(_this._fileIndex); } ; _AN_Call_send("send", this._xhr); break ; } } , getAudioURL: function (urls){ var extension; if (typeof urls === 'string') { urls = [urls] ; } for (var i = 0; i < _AN_Read_length('length', urls); i++ ){ extension = urls[i].toLowerCase(); extension = extension.substr((Math.max(0, extension.lastIndexOf(".")) || Infinity) + 1); if (this.game.device.canPlayAudio(extension)) { return urls[i]; } } return null ; } , fileError: function (index){ this._fileList[index].loaded = true ; this._fileList[index].error = true ; this.onFileError.dispatch(this._fileList[index].key, this._fileList[index]); console.warn("Phaser.Loader error loading file: " + this._fileList[index].key + ' from URL ' + _AN_Read_url('url', this._fileList[index])); this.nextFile(index, false ); } , fileComplete: function (index){ if (!this._fileList[index]) { console.warn('Phaser.Loader fileComplete invalid index ' + index); return ; } var file = this._fileList[index]; file.loaded = true ; var loadNext = true ; var _this = this; switch (file.type){ case 'image': this.game.cache.addImage(file.key, _AN_Read_url('url', file), file.data); break ; case 'spritesheet': this.game.cache.addSpriteSheet(file.key, _AN_Read_url('url', file), file.data, file.frameWidth, file.frameHeight, file.frameMax, file.margin, file.spacing); break ; case 'textureatlas': if (file.atlasURL == null ) { this.game.cache.addTextureAtlas(file.key, _AN_Read_url('url', file), file.data, file.atlasData, file.format); } else { loadNext = false ; _AN_Call_open('open', this._xhr, "GET", _AN_Read_baseurl("baseURL", this) + file.atlasURL, true ); this._xhr.responseType = "text"; if (file.format == Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY || file.format == Phaser.Loader.TEXTURE_ATLAS_JSON_HASH) { this._xhr.onload = function (){ return _this.jsonLoadComplete(index); } ; } else if (file.format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING) { this._xhr.onload = function (){ return _this.xmlLoadComplete(index); } ; } else { throw new Error("Phaser.Loader. Invalid Texture Atlas format: " + file.format) } this._xhr.onerror = function (){ return _this.dataLoadError(index); } ; _AN_Call_send("send", this._xhr); } break ; case 'bitmapfont': if (file.xmlURL == null ) { this.game.cache.addBitmapFont(file.key, _AN_Read_url('url', file), file.data, file.xmlData, file.xSpacing, file.ySpacing); } else { loadNext = false ; _AN_Call_open('open', this._xhr, "GET", _AN_Read_baseurl("baseURL", this) + file.xmlURL, true ); this._xhr.responseType = "text"; this._xhr.onload = function (){ return _this.xmlLoadComplete(index); } ; this._xhr.onerror = function (){ return _this.dataLoadError(index); } ; _AN_Call_send("send", this._xhr); } break ; case 'audio': if (this.game.sound.usingWebAudio) { file.data = this._xhr.response; this.game.cache.addSound(file.key, _AN_Read_url('url', file), file.data, true , false ); if (file.autoDecode) { var that = this; var key = file.key; this.game.cache.updateSound(key, 'isDecoding', true ); this.game.sound.context.decodeAudioData(file.data, function (buffer){ if (buffer) { that.game.cache.decodedSound(key, buffer); that.game.sound.onSoundDecode.dispatch(key, that.game.cache.getSound(key)); } } ); } } else { file.data.removeEventListener('canplaythrough', Phaser.GAMES[this.game.id].load.fileComplete); this.game.cache.addSound(file.key, _AN_Read_url('url', file), file.data, false , true ); } break ; case 'text': file.data = this._xhr.responseText; this.game.cache.addText(file.key, _AN_Read_url('url', file), file.data); break ; case 'physics': var data = JSON.parse(this._xhr.responseText); this.game.cache.addPhysicsData(file.key, _AN_Read_url('url', file), data, file.format); break ; case 'script': file.data = _AN_Call_createelement('createElement', document, 'script'); file.data.language = 'javascript'; file.data.type = 'text/javascript'; file.data.defer = false ; _AN_Write_text('text', file.data, false , this._xhr.responseText); _AN_Call_appendchild('appendChild', document.head, file.data); if (file.callback) { file.data = file.callback.call(file.callbackContext, file.key, this._xhr.responseText); } 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) { this.nextFile(index, true ); } } , jsonLoadComplete: function (index){ if (!this._fileList[index]) { console.warn('Phaser.Loader jsonLoadComplete invalid index ' + index); return ; } var file = this._fileList[index]; var data = JSON.parse(this._xhr.responseText); file.loaded = true ; if (file.type === 'tilemap') { this.game.cache.addTilemap(file.key, _AN_Read_url('url', file), data, file.format); } else if (file.type === 'json') { this.game.cache.addJSON(file.key, _AN_Read_url('url', file), data); } else { this.game.cache.addTextureAtlas(file.key, _AN_Read_url('url', file), file.data, data, file.format); } this.nextFile(index, true ); } , csvLoadComplete: function (index){ if (!this._fileList[index]) { console.warn('Phaser.Loader csvLoadComplete invalid index ' + index); return ; } var file = this._fileList[index]; var data = this._xhr.responseText; file.loaded = true ; this.game.cache.addTilemap(file.key, _AN_Read_url('url', file), data, file.format); this.nextFile(index, true ); } , dataLoadError: function (index){ var file = this._fileList[index]; file.loaded = true ; file.error = true ; console.warn("Phaser.Loader dataLoadError: " + file.key); this.nextFile(index, true ); } , xmlLoadComplete: function (index){ var data = this._xhr.responseText; var xml; 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) { xml = undefined; } if (!xml || !xml.documentElement || _AN_Read_length('length', _AN_Call_getelementsbytagname('getElementsByTagName', xml, "parsererror"))) { throw new Error("Phaser.Loader. Invalid XML given") } var file = this._fileList[index]; file.loaded = true ; if (file.type == 'bitmapfont') { this.game.cache.addBitmapFont(file.key, _AN_Read_url('url', file), file.data, xml, file.xSpacing, file.ySpacing); } else if (file.type == 'textureatlas') { this.game.cache.addTextureAtlas(file.key, _AN_Read_url('url', file), file.data, xml, file.format); } this.nextFile(index, true ); } , nextFile: function (previousIndex, success){ this.progressFloat += this._progressChunk; this.progress = Math.round(this.progressFloat); if (this.progress > 100) { this.progress = 100; } if (this.preloadSprite !== null ) { if (this.preloadSprite.direction === 0) { this.preloadSprite.rect.width = Math.floor((this.preloadSprite.width / 100) * this.progress); this.preloadSprite.sprite.crop(this.preloadSprite.rect); } else { this.preloadSprite.rect.height = Math.floor((this.preloadSprite.height / 100) * this.progress); this.preloadSprite.sprite.crop(this.preloadSprite.rect); } } this.onFileComplete.dispatch(this.progress, this._fileList[previousIndex].key, success, this.totalLoadedFiles(), _AN_Read_length('length', this._fileList)); if (this.totalQueuedFiles() > 0) { this._fileIndex++ ; this.loadFile(); } else { this.hasLoaded = true ; this.isLoading = false ; this.removeAll(); this.onLoadComplete.dispatch(); } } , totalLoadedFiles: function (){ var total = 0; for (var i = 0; i < _AN_Read_length('length', this._fileList); i++ ){ if (this._fileList[i].loaded) { total++ ; } } return total; } , totalQueuedFiles: function (){ var total = 0; for (var i = 0; i < _AN_Read_length('length', this._fileList); i++ ){ if (this._fileList[i].loaded === false ) { total++ ; } } return total; } } ; Phaser.Loader.prototype.constructor = Phaser.Loader;