Phaser.Loader = function (game){ this.game = game; this.cache = game.cache; this.resetLocked = false ; this.isLoading = false ; this.hasLoaded = false ; this.preloadSprite = null ; this.crossOrigin = false ; _AN_Write_baseurl('baseURL', this, false , ''); this.path = ''; this.onLoadStart = new Phaser.Signal(); this.onLoadComplete = new Phaser.Signal(); this.onPackComplete = new Phaser.Signal(); this.onFileStart = new Phaser.Signal(); this.onFileComplete = new Phaser.Signal(); this.onFileError = new Phaser.Signal(); this.useXDomainRequest = false ; this._warnedAboutXDomainRequest = false ; this.enableParallel = true ; this.maxParallelDownloads = 4; this._withSyncPointDepth = 0; this._fileList = [] ; this._flightQueue = [] ; this._processingHead = 0; this._fileLoadStarted = false ; this._totalPackCount = 0; this._totalFileCount = 0; this._loadedPackCount = 0; this._loadedFileCount = 0; } ; 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.TEXTURE_ATLAS_JSON_PYXEL = 5; 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 ; } , resize: function (){ if (this.preloadSprite && this.preloadSprite.height !== this.preloadSprite.sprite.height) { this.preloadSprite.rect.height = this.preloadSprite.sprite.height; } } , checkKeyExists: function (type, key){ return this.getAssetIndex(type, key) > -1; } , getAssetIndex: function (type, key){ var bestFound = -1; for (var i = 0; i < _AN_Read_length('length', this._fileList); i++ ){ var file = this._fileList[i]; if (file.type === type && file.key === key) { bestFound = i; if (!file.loaded && !file.loading) { break ; } } } return bestFound; } , getAsset: function (type, key){ var fileIndex = this.getAssetIndex(type, key); if (fileIndex > -1) { return { index: fileIndex, file: this._fileList[fileIndex]} ; } return false ; } , 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(); } } , 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; } , replaceInFileList: function (type, key, url, properties){ return this.addToFileList(type, key, url, properties, true ); } , 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} ; if (data) { if (typeof data === 'string') { data = JSON.parse(data); } pack.data = data || { } ; pack.loaded = true ; } for (var i = 0; i < _AN_Read_length('length', this._fileList) + 1; i++ ){ var file = this._fileList[i]; if (!file || (!file.loaded && !file.loading && file.type !== 'packfile')) { this._fileList.splice(i, 1, pack); this._totalPackCount++ ; break ; } } return this; } , image: function (key, url, overwrite){ return this.addToFileList('image', key, url, undefined, overwrite, '.png'); } , images: function (keys, urls){ if (Array.isArray(urls)) { for (var i = 0; i < _AN_Read_length('length', keys); i++ ){ this.image(keys[i], urls[i]); } } else { for (var i = 0; i < _AN_Read_length('length', keys); i++ ){ this.image(keys[i]); } } return this; } , text: function (key, url, overwrite){ return this.addToFileList('text', key, url, undefined, overwrite, '.txt'); } , json: function (key, url, overwrite){ return this.addToFileList('json', key, url, undefined, overwrite, '.json'); } , shader: function (key, url, overwrite){ return this.addToFileList('shader', key, url, undefined, overwrite, '.frag'); } , xml: function (key, url, overwrite){ return this.addToFileList('xml', key, url, undefined, overwrite, '.xml'); } , 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'); } , binary: function (key, url, callback, callbackContext){ if (callback === undefined) { callback = false ; } if (callback !== false && callbackContext === undefined) { callbackContext = callback; } return this.addToFileList('binary', key, url, { callback: callback, callbackContext: callbackContext} , false , '.bin'); } , spritesheet: function (key, url, frameWidth, frameHeight, frameMax, margin, spacing){ if (frameMax === undefined) { frameMax = -1; } if (margin === undefined) { margin = 0; } if (spacing === undefined) { spacing = 0; } return this.addToFileList('spritesheet', key, url, { frameWidth: frameWidth, frameHeight: frameHeight, frameMax: frameMax, margin: margin, spacing: spacing} , false , '.png'); } , 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} ); } , 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; } , video: function (key, urls, loadEvent, asBlob){ if (loadEvent === undefined) { if (this.game.device.firefox) { loadEvent = 'loadeddata'; } else { loadEvent = 'canplaythrough'; } } if (asBlob === undefined) { asBlob = false ; } if (typeof urls === 'string') { urls = [urls] ; } return this.addToFileList('video', key, urls, { buffer: null , asBlob: asBlob, loadEvent: loadEvent} ); } , tilemap: function (key, url, data, format){ if (url === undefined) { url = null ; } if (data === undefined) { data = null ; } if (format === undefined) { format = Phaser.Tilemap.CSV; } if (!url && !data) { if (format === Phaser.Tilemap.CSV) { url = key + '.csv'; } else { url = key + '.json'; } } if (data) { switch (format){ case Phaser.Tilemap.CSV: break ; case Phaser.Tilemap.TILED_JSON: if (typeof data === 'string') { data = JSON.parse(data); } break ; } this.cache.addTilemap(key, null , data, format); } else { this.addToFileList('tilemap', key, url, { format: format} ); } return this; } , physics: function (key, url, data, format){ if (url === undefined) { url = null ; } if (data === undefined) { data = null ; } if (format === undefined) { format = Phaser.Physics.LIME_CORONA_JSON; } if (!url && !data) { url = key + '.json'; } if (data) { if (typeof data === 'string') { data = JSON.parse(data); } this.cache.addPhysicsData(key, null , data, format); } else { this.addToFileList('physics', key, url, { format: format} ); } return this; } , bitmapFont: function (key, textureURL, atlasURL, atlasData, xSpacing, ySpacing){ if (textureURL === undefined || textureURL === null ) { textureURL = key + '.png'; } if (atlasURL === undefined) { atlasURL = null ; } if (atlasData === undefined) { atlasData = null ; } if (atlasURL === null && atlasData === null ) { atlasURL = key + '.xml'; } if (xSpacing === undefined) { xSpacing = 0; } if (ySpacing === undefined) { ySpacing = 0; } if (atlasURL) { this.addToFileList('bitmapfont', key, textureURL, { atlasURL: atlasURL, xSpacing: xSpacing, ySpacing: ySpacing} ); } else { if (typeof atlasData === 'string') { var json, xml; try { json = JSON.parse(atlasData); } catch (e) { xml = this.parseXml(atlasData); } if (!xml && !json) { throw new Error("Phaser.Loader. Invalid Bitmap Font atlas given") } this.addToFileList('bitmapfont', key, textureURL, { atlasURL: null , atlasData: json || xml, atlasType: (!!json? 'json': '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){ if (atlasURL === undefined) { atlasURL = null ; } if (atlasData === undefined) { atlasData = null ; } if (!atlasURL && !atlasData) { atlasURL = key + '.xml'; } return this.atlas(key, textureURL, atlasURL, atlasData, Phaser.Loader.TEXTURE_ATLAS_XML_STARLING); } , atlas: function (key, textureURL, atlasURL, atlasData, format){ if (textureURL === undefined || textureURL === null ) { textureURL = key + '.png'; } if (atlasURL === undefined) { atlasURL = null ; } if (atlasData === undefined) { atlasData = null ; } if (format === undefined) { format = Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY; } if (!atlasURL && !atlasData) { if (format === Phaser.Loader.TEXTURE_ATLAS_XML_STARLING) { atlasURL = key + '.xml'; } else { atlasURL = key + '.json'; } } 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 = this.parseXml(atlasData); if (!xml) { throw new Error("Phaser.Loader. Invalid Texture Atlas XML given") } atlasData = xml; } break ; } this.addToFileList('textureatlas', key, textureURL, { atlasURL: null , atlasData: atlasData, format: format} ); } return this; } , withSyncPoint: function (callback, callbackContext){ this._withSyncPointDepth++ ; try { callback.call(callbackContext || this, this); } finally{ this._withSyncPointDepth-- ; } return this; } , addSyncPoint: function (type, key){ var asset = this.getAsset(type, key); if (asset) { asset.file.syncPoint = true ; } return this; } , removeFile: function (type, key){ var asset = this.getAsset(type, key); if (asset) { if (!asset.loaded && !asset.loading) { this._fileList.splice(asset.index, 1); } } } , removeAll: function (){ this._fileList.length = 0; this._flightQueue.length = 0; } , start: function (){ if (this.isLoading) { return ; } this.hasLoaded = false ; this.isLoading = true ; this.updateProgress(); this.processLoadQueue(); } , processLoadQueue: function (){ if (!this.isLoading) { console.warn('Phaser.Loader - active loading canceled / reset'); this.finishedLoading(true ); return ; } for (var i = 0; i < _AN_Read_length('length', this._flightQueue); i++ ){ var file = this._flightQueue[i]; if (file.loaded || file.error) { this._flightQueue.splice(i, 1); i-- ; file.loading = false ; file.requestUrl = null ; file.requestObject = null ; if (file.error) { this.onFileError.dispatch(file.key, file); } if (file.type !== 'packfile') { this._loadedFileCount++ ; this.onFileComplete.dispatch(this.progress, file.key, !file.error, this._loadedFileCount, this._totalFileCount); } else if (file.type === 'packfile' && file.error) { this._loadedPackCount++ ; this.onPackComplete.dispatch(file.key, !file.error, this._loadedPackCount, this._totalPackCount); } } } var syncblock = false ; var inflightLimit = this.enableParallel? Phaser.Math.clamp(this.maxParallelDownloads, 1, 12): 1; for (var i = this._processingHead; i < _AN_Read_length('length', this._fileList); i++ ){ var file = this._fileList[i]; if (file.type === 'packfile' && !file.error && file.loaded && i === this._processingHead) { this.processPack(file); this._loadedPackCount++ ; this.onPackComplete.dispatch(file.key, !file.error, this._loadedPackCount, this._totalPackCount); } if (file.loaded || file.error) { if (i === this._processingHead) { this._processingHead = i + 1; } } else if (!file.loading && _AN_Read_length('length', this._flightQueue) < inflightLimit) { if (file.type === 'packfile' && !file.data) { this._flightQueue.push(file); file.loading = true ; this.loadFile(file); } else if (!syncblock) { if (!this._fileLoadStarted) { this._fileLoadStarted = true ; this.onLoadStart.dispatch(); } this._flightQueue.push(file); file.loading = true ; this.onFileStart.dispatch(this.progress, file.key, _AN_Read_url('url', file)); this.loadFile(file); } } if (!file.loaded && file.syncPoint) { syncblock = true ; } if (_AN_Read_length('length', this._flightQueue) >= inflightLimit || (syncblock && this._loadedPackCount === this._totalPackCount)) { break ; } } this.updateProgress(); if (this._processingHead >= _AN_Read_length('length', this._fileList)) { this.finishedLoading(); } else if (!_AN_Read_length('length', this._flightQueue)) { console.warn("Phaser.Loader - aborting: processing queue empty, loading may have stalled"); var _this = this; _AN_Call_settimeout("setTimeout", window, function (){ _this.finishedLoading(true ); } , 2000); } } , finishedLoading: function (abnormal){ if (this.hasLoaded) { return ; } this.hasLoaded = true ; this.isLoading = false ; if (!abnormal && !this._fileLoadStarted) { this._fileLoadStarted = true ; this.onLoadStart.dispatch(); } this.onLoadComplete.dispatch(); this.reset(); this.game.state.loadComplete(); } , asyncComplete: function (file, errorMessage){ if (errorMessage === undefined) { errorMessage = ''; } file.loaded = true ; file.error = !!errorMessage; if (errorMessage) { file.errorMessage = errorMessage; console.warn('Phaser.Loader - ' + file.type + '[' + file.key + ']' + ': ' + errorMessage); } this.processLoadQueue(); } , processPack: function (pack){ var packData = pack.data[pack.key]; if (!packData) { console.warn('Phaser.Loader - ' + pack.key + ': pack has data, but not for pack key'); return ; } for (var i = 0; i < _AN_Read_length('length', packData); i++ ){ var file = packData[i]; switch (file.type){ case "image": this.image(file.key, _AN_Read_url("url", file), file.overwrite); break ; case "text": this.text(file.key, _AN_Read_url("url", file), file.overwrite); break ; case "json": this.json(file.key, _AN_Read_url("url", file), file.overwrite); break ; case "xml": this.xml(file.key, _AN_Read_url("url", file), file.overwrite); break ; case "script": this.script(file.key, _AN_Read_url("url", file), file.callback, pack.callbackContext || this); break ; case "binary": this.binary(file.key, _AN_Read_url("url", file), file.callback, pack.callbackContext || this); break ; case "spritesheet": this.spritesheet(file.key, _AN_Read_url("url", file), file.frameWidth, file.frameHeight, file.frameMax, file.margin, file.spacing); break ; case "video": this.video(file.key, file.urls); break ; case "audio": this.audio(file.key, file.urls, file.autoDecode); break ; case "audiosprite": this.audiosprite(file.key, file.urls, file.jsonURL, file.jsonData, file.autoDecode); break ; case "tilemap": this.tilemap(file.key, _AN_Read_url("url", file), file.data, Phaser.Tilemap[file.format]); break ; case "physics": this.physics(file.key, _AN_Read_url("url", file), file.data, Phaser.Loader[file.format]); break ; case "bitmapFont": this.bitmapFont(file.key, file.textureURL, file.atlasURL, file.atlasData, file.xSpacing, file.ySpacing); break ; case "atlasJSONArray": this.atlasJSONArray(file.key, file.textureURL, file.atlasURL, file.atlasData); break ; case "atlasJSONHash": this.atlasJSONHash(file.key, file.textureURL, file.atlasURL, file.atlasData); break ; case "atlasXML": this.atlasXML(file.key, file.textureURL, file.atlasURL, file.atlasData); break ; case "atlas": this.atlas(file.key, file.textureURL, file.atlasURL, file.atlasData, Phaser.Loader[file.format]); break ; case "shader": this.shader(file.key, _AN_Read_url("url", file), file.overwrite); break ; } } } , transformUrl: function (url, file){ if (!url) { return false ; } if (url.match(/^(?:blob:|data:|http:\/\/|https:\/\/|\/\/)/)) { return url; } else { return _AN_Read_baseurl("baseURL", this) + file.path + url; } } , loadFile: function (file){ switch (file.type){ case 'packfile': this.xhrLoad(file, this.transformUrl(_AN_Read_url('url', file), file), 'text', this.fileComplete); break ; case 'image': case 'spritesheet': case 'textureatlas': case 'bitmapfont': this.loadImageTag(file); break ; case 'audio': _AN_Write_url('url', file, false , this.getAudioURL(_AN_Read_url('url', file))); if (file.url) { if (this.game.sound.usingWebAudio) { this.xhrLoad(file, this.transformUrl(_AN_Read_url('url', file), file), 'arraybuffer', this.fileComplete); } else if (this.game.sound.usingAudioTag) { this.loadAudioTag(file); } } else { this.fileError(file, null , 'No supported audio URL specified or device does not have audio playback support'); } break ; case 'video': _AN_Write_url('url', file, false , this.getVideoURL(_AN_Read_url('url', file))); if (file.url) { if (file.asBlob) { this.xhrLoad(file, this.transformUrl(_AN_Read_url('url', file), file), 'arraybuffer', this.fileComplete); } else { this.loadVideoTag(file); } } else { this.fileError(file, null , 'No supported video URL specified or device does not have video playback support'); } break ; case 'json': this.xhrLoad(file, this.transformUrl(_AN_Read_url('url', file), file), 'text', this.jsonLoadComplete); break ; case 'xml': this.xhrLoad(file, this.transformUrl(_AN_Read_url('url', file), file), 'text', this.xmlLoadComplete); break ; case 'tilemap': if (file.format === Phaser.Tilemap.TILED_JSON) { this.xhrLoad(file, this.transformUrl(_AN_Read_url('url', file), file), 'text', this.jsonLoadComplete); } else if (file.format === Phaser.Tilemap.CSV) { this.xhrLoad(file, this.transformUrl(_AN_Read_url('url', file), file), 'text', this.csvLoadComplete); } else { this.asyncComplete(file, "invalid Tilemap format: " + file.format); } break ; case 'text': case 'script': case 'shader': case 'physics': this.xhrLoad(file, this.transformUrl(_AN_Read_url('url', file), file), 'text', this.fileComplete); break ; case 'binary': this.xhrLoad(file, this.transformUrl(_AN_Read_url('url', file), file), 'arraybuffer', this.fileComplete); break ; } } , loadImageTag: function (file){ var _this = this; file.data = new Image(); file.data.name = file.key; if (this.crossOrigin) { file.data.crossOrigin = this.crossOrigin; } file.data.onload = function (){ if (file.data.onload) { file.data.onload = null ; file.data.onerror = null ; _this.fileComplete(file); } } ; file.data.onerror = function (){ if (file.data.onload) { file.data.onload = null ; file.data.onerror = null ; _this.fileError(file); } } ; _AN_Write_src('src', file.data, false , this.transformUrl(_AN_Read_url('url', file), file)); if (file.data.complete && file.data.width && file.data.height) { file.data.onload = null ; file.data.onerror = null ; this.fileComplete(file); } } , loadVideoTag: function (file){ var _this = this; file.data = _AN_Call_createelement('createElement', document, "video"); file.data.name = file.key; file.data.controls = false ; file.data.autoplay = false ; var videoLoadEvent = function (){ file.data.removeEventListener(file.loadEvent, videoLoadEvent, false ); file.data.onerror = null ; file.data.canplay = true ; Phaser.GAMES[_this.game.id].load.fileComplete(file); } ; file.data.onerror = function (){ file.data.removeEventListener(file.loadEvent, videoLoadEvent, false ); file.data.onerror = null ; file.data.canplay = false ; _this.fileError(file); } ; file.data.addEventListener(file.loadEvent, videoLoadEvent, false ); _AN_Write_src("src", file.data, false , this.transformUrl(_AN_Read_url("url", file), file)); _AN_Call_load("load", file.data); } , loadAudioTag: function (file){ var _this = this; 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 , this.transformUrl(_AN_Read_url('url', file), file)); this.fileComplete(file); } else { file.data = new Audio(); file.data.name = file.key; var playThroughEvent = function (){ file.data.removeEventListener('canplaythrough', playThroughEvent, false ); file.data.onerror = null ; Phaser.GAMES[_this.game.id].load.fileComplete(file); } ; file.data.onerror = function (){ file.data.removeEventListener('canplaythrough', playThroughEvent, false ); file.data.onerror = null ; _this.fileError(file); } ; file.data.preload = 'auto'; _AN_Write_src('src', file.data, false , this.transformUrl(_AN_Read_url('url', file), file)); file.data.addEventListener('canplaythrough', playThroughEvent, false ); _AN_Call_load('load', file.data); } } , xhrLoad: function (file, url, type, onload, onerror){ if (this.useXDomainRequest && window.XDomainRequest) { this.xhrLoadWithXDR(file, url, type, onload, onerror); return ; } var xhr = new XMLHttpRequest(); _AN_Call_open('open', xhr, "GET", url, true ); xhr.responseType = type; onerror = onerror || this.fileError; var _this = this; xhr.onload = function (){ try { return onload.call(_this, file, xhr); } catch (e) { if (!_this.hasLoaded) { _this.asyncComplete(file, e.message || 'Exception'); } else { if (window.console) { console.error(e); } } } } ; xhr.onerror = function (){ try { return onerror.call(_this, file, xhr); } catch (e) { if (!_this.hasLoaded) { _this.asyncComplete(file, e.message || 'Exception'); } else { if (window.console) { console.error(e); } } } } ; file.requestObject = xhr; file.requestUrl = url; _AN_Call_send('send', xhr); } , xhrLoadWithXDR: function (file, url, type, onload, onerror){ if (!this._warnedAboutXDomainRequest && (!this.game.device.ie || this.game.device.ieVersion >= 10)) { this._warnedAboutXDomainRequest = true ; console.warn("Phaser.Loader - using XDomainRequest outside of IE 9"); } var xhr = new window.XDomainRequest(); _AN_Call_open("open", xhr, 'GET', url, true ); xhr.responseType = type; xhr.timeout = 3000; onerror = onerror || this.fileError; var _this = this; xhr.onerror = function (){ try { return onerror.call(_this, file, xhr); } catch (e) { _this.asyncComplete(file, e.message || 'Exception'); } } ; xhr.ontimeout = function (){ try { return onerror.call(_this, file, xhr); } catch (e) { _this.asyncComplete(file, e.message || 'Exception'); } } ; xhr.onprogress = function (){ } ; xhr.onload = function (){ try { return onload.call(_this, file, xhr); } catch (e) { _this.asyncComplete(file, e.message || 'Exception'); } } ; file.requestObject = xhr; file.requestUrl = url; _AN_Call_settimeout('setTimeout', window, function (){ _AN_Call_send('send', xhr); } , 0); } , getVideoURL: function (urls){ for (var i = 0; i < _AN_Read_length('length', urls); i++ ){ var url = urls[i]; var videoType; if (url.uri) { url = url.uri; videoType = url.type; } else { if (url.indexOf("blob:") === 0 || url.indexOf("data:") === 0) { return url; } if (url.indexOf("?") >= 0) { url = url.substr(0, url.indexOf("?")); } var extension = url.substr((Math.max(0, url.lastIndexOf(".")) || Infinity) + 1); videoType = extension.toLowerCase(); } if (this.game.device.canPlayVideo(videoType)) { return urls[i]; } } return null ; } , getAudioURL: function (urls){ if (this.game.sound.noAudio) { return null ; } for (var i = 0; i < _AN_Read_length("length", urls); i++ ){ var url = urls[i]; var audioType; if (url.uri) { url = url.uri; audioType = url.type; } else { if (url.indexOf("blob:") === 0 || url.indexOf("data:") === 0) { return url; } if (url.indexOf("?") >= 0) { url = url.substr(0, url.indexOf("?")); } var extension = url.substr((Math.max(0, url.lastIndexOf(".")) || Infinity) + 1); audioType = extension.toLowerCase(); } if (this.game.device.canPlayAudio(audioType)) { return urls[i]; } } return null ; } , fileError: function (file, xhr, reason){ var url = file.requestUrl || this.transformUrl(_AN_Read_url("url", file), file); var message = 'error loading asset from URL ' + url; if (!reason && xhr) { reason = xhr.status; } if (reason) { message = message + ' (' + reason + ')'; } this.asyncComplete(file, message); } , fileComplete: function (file, xhr){ var loadNext = true ; switch (file.type){ case 'packfile': var data = JSON.parse(xhr.responseText); file.data = data || { } ; break ; case 'image': this.cache.addImage(file.key, _AN_Read_url('url', file), file.data); break ; case 'spritesheet': this.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.cache.addTextureAtlas(file.key, _AN_Read_url('url', file), file.data, file.atlasData, file.format); } else { loadNext = false ; if (file.format == Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY || file.format == Phaser.Loader.TEXTURE_ATLAS_JSON_HASH || file.format == Phaser.Loader.TEXTURE_ATLAS_JSON_PYXEL) { this.xhrLoad(file, this.transformUrl(file.atlasURL, file), 'text', this.jsonLoadComplete); } else if (file.format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING) { this.xhrLoad(file, this.transformUrl(file.atlasURL, file), 'text', this.xmlLoadComplete); } else { throw new Error("Phaser.Loader. Invalid Texture Atlas format: " + file.format) } } break ; case 'bitmapfont': if (!file.atlasURL) { this.cache.addBitmapFont(file.key, _AN_Read_url('url', file), file.data, file.atlasData, file.atlasType, file.xSpacing, file.ySpacing); } else { loadNext = false ; this.xhrLoad(file, this.transformUrl(file.atlasURL, file), 'text', function (file, xhr){ var json; try { json = JSON.parse(xhr.responseText); } catch (e) { } if (!!json) { file.atlasType = 'json'; this.jsonLoadComplete(file, xhr); } else { file.atlasType = 'xml'; this.xmlLoadComplete(file, xhr); } } ); } break ; case 'video': if (file.asBlob) { try { file.data = new Blob([new Uint8Array(xhr.response)] ); } catch (e) { throw new Error("Phaser.Loader. Unable to parse video file as Blob: " + file.key) } } this.cache.addVideo(file.key, _AN_Read_url("url", file), file.data, file.asBlob); break ; case 'audio': if (this.game.sound.usingWebAudio) { file.data = xhr.response; this.cache.addSound(file.key, _AN_Read_url('url', file), file.data, true , false ); if (file.autoDecode) { this.game.sound.decode(file.key); } } else { this.cache.addSound(file.key, _AN_Read_url('url', file), file.data, false , true ); } break ; case 'text': file.data = xhr.responseText; this.cache.addText(file.key, _AN_Read_url('url', file), file.data); break ; case 'shader': file.data = xhr.responseText; this.cache.addShader(file.key, _AN_Read_url('url', file), file.data); break ; case 'physics': var data = JSON.parse(xhr.responseText); this.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 , xhr.responseText); _AN_Call_appendchild('appendChild', document.head, file.data); if (file.callback) { file.data = file.callback.call(file.callbackContext, file.key, xhr.responseText); } break ; case 'binary': if (file.callback) { file.data = file.callback.call(file.callbackContext, file.key, xhr.response); } else { file.data = xhr.response; } this.cache.addBinary(file.key, file.data); break ; } if (loadNext) { this.asyncComplete(file); } } , jsonLoadComplete: function (file, xhr){ var data = JSON.parse(xhr.responseText); if (file.type === 'tilemap') { this.cache.addTilemap(file.key, _AN_Read_url('url', file), data, file.format); } else if (file.type === 'bitmapfont') { this.cache.addBitmapFont(file.key, _AN_Read_url('url', file), file.data, data, file.atlasType, file.xSpacing, file.ySpacing); } else if (file.type === 'json') { this.cache.addJSON(file.key, _AN_Read_url('url', file), data); } else { this.cache.addTextureAtlas(file.key, _AN_Read_url('url', file), file.data, data, file.format); } this.asyncComplete(file); } , csvLoadComplete: function (file, xhr){ var data = xhr.responseText; this.cache.addTilemap(file.key, _AN_Read_url('url', file), data, file.format); this.asyncComplete(file); } , xmlLoadComplete: function (file, xhr){ var data = xhr.responseText; var xml = this.parseXml(data); if (!xml) { var responseType = xhr.responseType || xhr.contentType; console.warn('Phaser.Loader - ' + file.key + ': invalid XML (' + responseType + ')'); this.asyncComplete(file, "invalid XML"); return ; } if (file.type === 'bitmapfont') { this.cache.addBitmapFont(file.key, _AN_Read_url('url', file), file.data, xml, file.atlasType, file.xSpacing, file.ySpacing); } else if (file.type === 'textureatlas') { this.cache.addTextureAtlas(file.key, _AN_Read_url('url', file), file.data, xml, file.format); } else if (file.type === 'xml') { this.cache.addXML(file.key, _AN_Read_url('url', file), xml); } this.asyncComplete(file); } , parseXml: function (data){ 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 = null ; } if (!xml || !xml.documentElement || _AN_Read_length('length', _AN_Call_getelementsbytagname('getElementsByTagName', xml, "parsererror"))) { return null ; } else { return xml; } } , updateProgress: function (){ if (this.preloadSprite) { if (this.preloadSprite.direction === 0) { this.preloadSprite.rect.width = Math.floor((this.preloadSprite.width / 100) * this.progress); } else { this.preloadSprite.rect.height = Math.floor((this.preloadSprite.height / 100) * this.progress); } if (this.preloadSprite.sprite) { this.preloadSprite.sprite.updateCrop(); } else { this.preloadSprite = null ; } } } , totalLoadedFiles: function (){ return this._loadedFileCount; } , totalQueuedFiles: function (){ return this._totalFileCount - this._loadedFileCount; } , totalLoadedPacks: function (){ return this._totalPackCount; } , totalQueuedPacks: function (){ return this._totalPackCount - this._loadedPackCount; } } ; Object.defineProperty(Phaser.Loader.prototype, "progressFloat", { get: function (){ var progress = (this._loadedFileCount / this._totalFileCount) * 100; return Phaser.Math.clamp(progress || 0, 0, 100); } } ); Object.defineProperty(Phaser.Loader.prototype, "progress", { get: function (){ return Math.round(this.progressFloat); } } ); Phaser.Loader.prototype.constructor = Phaser.Loader;