Phaser.Loader = function (game){ this.game = game; this._keys = [] ; this._fileList = { } ; this._progressChunk = 0; this._xhr = new XMLHttpRequest(); this.queueSize = 0; this.isLoading = false ; this.hasLoaded = false ; this.progress = 0; this.preloadSprite = null ; this.crossOrigin = ''; _AN_Write_baseurl('baseURL', this, false , ''); this.onFileComplete = new Phaser.Signal(); this.onFileError = new Phaser.Signal(); this.onLoadStart = 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.prototype = { setPreloadSprite: function (sprite, direction){ direction = direction || 0; this.preloadSprite = { sprite: sprite, direction: direction, width: sprite.width, height: sprite.height, crop: null } ; if (direction == 0) { this.preloadSprite.crop = new Phaser.Rectangle(0, 0, 0, sprite.height); } else { this.preloadSprite.crop = new Phaser.Rectangle(0, 0, sprite.width, 0); } sprite.crop = this.preloadSprite.crop; } , checkKeyExists: function (key){ if (this._fileList[key]) { return true ; } else { return false ; } } , reset: function (){ this.preloadSprite = null ; this.queueSize = 0; this.isLoading = false ; } , 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]; } } this._fileList[key] = entry; this._keys.push(key); this.queueSize++ ; } , image: function (key, url, overwrite){ if (typeof overwrite === "undefined") { overwrite = false ; } if (overwrite || this.checkKeyExists(key) == false ) { this.addToFileList('image', key, url); } } , text: function (key, url, overwrite){ if (typeof overwrite === "undefined") { overwrite = false ; } if (overwrite || this.checkKeyExists(key) == false ) { this.addToFileList('text', key, url); } } , spritesheet: function (key, url, frameWidth, frameHeight, frameMax){ if (typeof frameMax === "undefined") { frameMax = -1; } if (this.checkKeyExists(key) === false ) { this.addToFileList('spritesheet', key, url, { frameWidth: frameWidth, frameHeight: frameHeight, frameMax: frameMax} ); } } , audio: function (key, urls, autoDecode){ if (typeof autoDecode === "undefined") { autoDecode = true ; } if (this.checkKeyExists(key) === false ) { this.addToFileList('audio', key, urls, { buffer: null , autoDecode: autoDecode} ); } } , tilemap: function (key, tilesetURL, mapDataURL, mapData, format){ if (typeof mapDataURL === "undefined") { mapDataURL = null ; } if (typeof mapData === "undefined") { mapData = null ; } if (typeof format === "undefined") { format = Phaser.Tilemap.CSV; } if (this.checkKeyExists(key) === false ) { if (mapDataURL) { this.addToFileList('tilemap', key, tilesetURL, { mapDataURL: mapDataURL, format: format} ); } else { switch (format){ case Phaser.Tilemap.CSV: break ; case Phaser.Tilemap.JSON: if (typeof mapData === 'string') { mapData = JSON.parse(mapData); } break ; } this.addToFileList('tilemap', key, tilesetURL, { mapDataURL: null , mapData: mapData, format: format} ); } } } , bitmapFont: function (key, textureURL, xmlURL, xmlData){ if (typeof xmlURL === "undefined") { xmlURL = null ; } if (typeof xmlData === "undefined") { xmlData = null ; } if (this.checkKeyExists(key) === false ) { if (xmlURL) { this.addToFileList('bitmapfont', key, textureURL, { xmlURL: xmlURL} ); } 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} ); } } } } } , atlasJSONArray: function (key, textureURL, atlasURL, atlasData){ this.atlas(key, textureURL, atlasURL, atlasData, Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY); } , atlasJSONHash: function (key, textureURL, atlasURL, atlasData){ this.atlas(key, textureURL, atlasURL, atlasData, Phaser.Loader.TEXTURE_ATLAS_JSON_HASH); } , atlasXML: function (key, textureURL, atlasURL, atlasData){ 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 (this.checkKeyExists(key) === false ) { 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} ); } } } , removeFile: function (key){ delete this._fileList[key]; } , removeAll: function (){ this._fileList = { } ; } , start: function (){ if (this.isLoading) { return ; } this.progress = 0; this.hasLoaded = false ; this.isLoading = true ; this.onLoadStart.dispatch(this.queueSize); if (_AN_Read_length('length', this._keys) > 0) { this._progressChunk = 100 / _AN_Read_length('length', this._keys); this.loadFile(); } else { this.progress = 100; this.hasLoaded = true ; this.onLoadComplete.dispatch(); } } , loadFile: function (){ var file = this._fileList[this._keys.shift()]; var _this = this; switch (file.type){ case 'image': case 'spritesheet': case 'textureatlas': case 'bitmapfont': case 'tilemap': file.data = new Image(); file.data.name = file.key; file.data.onload = function (){ return _this.fileComplete(file.key); } ; file.data.onerror = function (){ return _this.fileError(file.key); } ; 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(file.key); } ; this._xhr.onerror = function (){ return _this.fileError(file.key); } ; _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(file.key); } else { file.data = new Audio(); file.data.name = file.key; file.data.onerror = function (){ return _this.fileError(file.key); } ; 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(file.key), false ); _AN_Call_load('load', file.data); } } } else { this.fileError(file.key); } break ; case 'text': _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(file.key); } ; this._xhr.onerror = function (){ return _this.fileError(file.key); } ; _AN_Call_send("send", this._xhr); break ; } } , getAudioURL: function (urls){ var extension; 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 (key){ this._fileList[key].loaded = true ; this._fileList[key].error = true ; this.onFileError.dispatch(key); console.warn("Phaser.Loader error loading file: " + key); this.nextFile(key, false ); } , fileComplete: function (key){ if (!this._fileList[key]) { console.warn('Phaser.Loader fileComplete invalid key ' + key); return ; } this._fileList[key].loaded = true ; var file = this._fileList[key]; 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); break ; case 'tilemap': if (file.mapDataURL == null ) { this.game.cache.addTilemap(file.key, _AN_Read_url('url', file), file.data, file.mapData, file.format); } else { loadNext = false ; _AN_Call_open('open', this._xhr, "GET", _AN_Read_baseurl("baseURL", this) + file.mapDataURL, true ); this._xhr.responseType = "text"; if (file.format == Phaser.Tilemap.JSON) { this._xhr.onload = function (){ return _this.jsonLoadComplete(file.key); } ; } else if (file.format == Phaser.Tilemap.CSV) { this._xhr.onload = function (){ return _this.csvLoadComplete(file.key); } ; } this._xhr.onerror = function (){ return _this.dataLoadError(file.key); } ; _AN_Call_send("send", this._xhr); } 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(file.key); } ; } else if (file.format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING) { this._xhr.onload = function (){ return _this.xmlLoadComplete(file.key); } ; } this._xhr.onerror = function (){ return _this.dataLoadError(file.key); } ; _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); } 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(file.key); } ; this._xhr.onerror = function (){ return _this.dataLoadError(file.key); } ; _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) { this.game.cache.updateSound(key, 'isDecoding', true ); var that = this; var key = file.key; this.game.sound.context.decodeAudioData(file.data, function (buffer){ if (buffer) { that.game.cache.decodedSound(key, buffer); } } ); } } 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.response; this.game.cache.addText(file.key, _AN_Read_url('url', file), file.data); break ; } if (loadNext) { this.nextFile(key, true ); } } , jsonLoadComplete: function (key){ var data = JSON.parse(this._xhr.response); var file = this._fileList[key]; if (file.type == 'tilemap') { this.game.cache.addTilemap(file.key, _AN_Read_url('url', file), file.data, data, file.format); } else { this.game.cache.addTextureAtlas(file.key, _AN_Read_url('url', file), file.data, data, file.format); } this.nextFile(key, true ); } , csvLoadComplete: function (key){ var data = this._xhr.response; var file = this._fileList[key]; this.game.cache.addTilemap(file.key, _AN_Read_url('url', file), file.data, data, file.format); this.nextFile(key, true ); } , dataLoadError: function (key){ var file = this._fileList[key]; file.error = true ; console.warn("Phaser.Loader dataLoadError: " + key); this.nextFile(key, true ); } , xmlLoadComplete: function (key){ var data = this._xhr.response; 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[key]; if (file.type == 'bitmapfont') { this.game.cache.addBitmapFont(file.key, _AN_Read_url('url', file), file.data, xml); } else if (file.type == 'textureatlas') { this.game.cache.addTextureAtlas(file.key, _AN_Read_url('url', file), file.data, xml, file.format); } this.nextFile(key, true ); } , nextFile: function (previousKey, success){ this.progress = Math.round(this.progress + this._progressChunk); if (this.progress > 100) { this.progress = 100; } if (this.preloadSprite !== null ) { if (this.preloadSprite.direction == 0) { this.preloadSprite.crop.width = (this.preloadSprite.width / 100) * this.progress; } else { this.preloadSprite.crop.height = (this.preloadSprite.height / 100) * this.progress; } this.preloadSprite.sprite.crop = this.preloadSprite.crop; } this.onFileComplete.dispatch(this.progress, previousKey, success, this.queueSize - _AN_Read_length('length', this._keys), this.queueSize); if (_AN_Read_length('length', this._keys) > 0) { this.loadFile(); } else { this.hasLoaded = true ; this.isLoading = false ; this.removeAll(); this.onLoadComplete.dispatch(); } } } ;