2013-05-25 03:21:24 +00:00
/// <reference path="../Game.ts" />
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
/ * *
2013-04-18 15:49:08 +00:00
* Phaser - Loader
*
* The Loader handles loading all external content such as Images , Sounds , Texture Atlases and data files .
* It uses a combination of Image ( ) loading and xhr and provides for progress and completion callbacks .
2013-04-18 13:16:18 +00:00
* /
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
module Phaser {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
export class Loader {
2013-04-12 16:19:56 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Loader constructor
*
* @param game { Phaser . Game } Current game instance .
* @param callback { function } This will be called when assets completely loaded .
* /
2013-04-18 13:16:18 +00:00
constructor ( game : Game , callback ) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this . _game = game ;
this . _gameCreateComplete = callback ;
this . _keys = [ ] ;
this . _fileList = { } ;
this . _xhr = new XMLHttpRequest ( ) ;
2013-04-20 02:40:17 +00:00
this . _queueSize = 0 ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Local private reference to game .
* /
2013-04-18 13:16:18 +00:00
private _game : Game ;
2013-05-17 05:49:43 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Array stors assets keys . So you can get that asset by its unique key .
* /
2013-04-18 13:16:18 +00:00
private _keys : string [ ] ;
2013-05-17 05:49:43 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Contains all the assets file infos .
* /
2013-04-18 13:16:18 +00:00
private _fileList ;
2013-05-17 05:49:43 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Game initialial assets loading callback .
* /
2013-04-18 13:16:18 +00:00
private _gameCreateComplete ;
private _onComplete ;
private _onFileLoad ;
2013-05-17 05:49:43 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Indicates assets loading progress . ( from 0 to 100 )
* @type { number }
* /
2013-04-18 13:16:18 +00:00
private _progressChunk : number ;
2013-05-17 05:49:43 +00:00
2013-04-18 13:16:18 +00:00
private _xhr : XMLHttpRequest ;
2013-05-17 05:49:43 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Length of assets queue .
* @type { number }
* /
2013-04-20 02:40:17 +00:00
private _queueSize : number ;
2013-04-12 16:19:56 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* True if game is completely loaded .
* @type { boolean }
* /
2013-04-18 13:16:18 +00:00
public hasLoaded : bool ;
2013-05-17 05:49:43 +00:00
2013-05-04 12:02:12 +00:00
/ * *
2013-07-12 02:28:46 +00:00
* Loading progress ( from 0 to 100 )
2013-05-04 12:02:12 +00:00
* @type { number }
* /
2013-04-18 13:16:18 +00:00
public progress : number ;
2013-04-12 16:19:56 +00:00
2013-05-22 23:01:58 +00:00
/ * *
* The crossOrigin value applied to loaded images
* @type { string }
* /
public crossOrigin : string = '' ;
2013-05-23 02:08:57 +00:00
/ * *
* TextureAtlas data format constants
* /
public static TEXTURE_ATLAS_JSON_ARRAY : number = 0 ;
public static TEXTURE_ATLAS_JSON_HASH : number = 1 ;
public static TEXTURE_ATLAS_XML_STARLING : number = 2 ;
2013-05-04 12:02:12 +00:00
/ * *
* Reset loader , this will remove all loaded assets .
* /
2013-04-20 02:40:17 +00:00
public reset() {
this . _queueSize = 0 ;
}
2013-04-12 16:19:56 +00:00
2013-04-20 02:40:17 +00:00
public get queueSize ( ) : number {
return this . _queueSize ;
2013-04-12 16:19:56 +00:00
}
2013-05-04 12:02:12 +00:00
/ * *
* Add a new image asset loading request with key and url .
* @param key { string } Unique asset key of this image file .
* @param url { string } URL of image file .
* /
2013-07-12 02:28:46 +00:00
public image ( key : string , url : string , overwrite : bool = false ) {
2013-04-12 16:19:56 +00:00
2013-07-12 02:28:46 +00:00
if ( overwrite == true || this . checkKeyExists ( key ) == false )
2013-04-18 13:16:18 +00:00
{
2013-04-20 02:40:17 +00:00
this . _queueSize ++ ;
2013-04-18 13:16:18 +00:00
this . _fileList [ key ] = { type : 'image' , key : key , url : url , data : null , error : false , loaded : false } ;
this . _keys . push ( key ) ;
}
2013-04-12 16:19:56 +00:00
}
2013-05-04 12:02:12 +00:00
/ * *
* Add a new sprite sheet loading request .
* @param key { string } Unique asset key of the sheet file .
* @param url { string } URL of sheet file .
* @param frameWidth { number } Width of each single frame .
* @param frameHeight { number } Height of each single frame .
* @param frameMax { number } How many frames in this sprite sheet .
* /
2013-06-05 01:58:16 +00:00
public spritesheet ( key : string , url : string , frameWidth : number , frameHeight : number , frameMax? : number = - 1 ) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if ( this . checkKeyExists ( key ) === false )
2013-04-12 16:19:56 +00:00
{
2013-04-20 02:40:17 +00:00
this . _queueSize ++ ;
2013-04-18 13:16:18 +00:00
this . _fileList [ key ] = { type : 'spritesheet' , key : key , url : url , data : null , frameWidth : frameWidth , frameHeight : frameHeight , frameMax : frameMax , error : false , loaded : false } ;
2013-04-12 16:19:56 +00:00
this . _keys . push ( key ) ;
}
2013-04-18 13:16:18 +00:00
}
2013-05-04 12:02:12 +00:00
/ * *
* Add a new texture atlas loading request .
* @param key { string } Unique asset key of the texture atlas file .
2013-05-23 02:08:57 +00:00
* @param textureURL { string } The url of the texture atlas image file .
* @param [ atlasURL ] { string } The url of the texture atlas data file ( json / xml )
* @param [ atlasData ] { object } A JSON or XML data object .
* @param [ format ] { number } A value describing the format of the data .
2013-05-04 12:02:12 +00:00
* /
2013-06-05 01:58:16 +00:00
public atlas ( key : string , textureURL : string , atlasURL? : string = null , atlasData ? = null , format? :number = Loader . TEXTURE_ATLAS_JSON_ARRAY ) {
2013-04-18 13:16:18 +00:00
if ( this . checkKeyExists ( key ) === false )
2013-04-12 16:19:56 +00:00
{
2013-05-23 02:08:57 +00:00
if ( atlasURL !== null )
2013-04-12 16:19:56 +00:00
{
2013-05-23 02:08:57 +00:00
// A URL to a json/xml file has been given
2013-04-20 02:40:17 +00:00
this . _queueSize ++ ;
2013-05-23 02:08:57 +00:00
this . _fileList [ key ] = { type : 'textureatlas' , key : key , url : textureURL , atlasURL : atlasURL , data : null , format : format , error : false , loaded : false } ;
2013-04-18 13:16:18 +00:00
this . _keys . push ( key ) ;
2013-04-12 16:19:56 +00:00
}
else
{
2013-05-23 02:08:57 +00:00
if ( format == Loader . TEXTURE_ATLAS_JSON_ARRAY )
2013-04-18 13:16:18 +00:00
{
2013-05-23 02:08:57 +00:00
// A json string or object has been given
if ( typeof atlasData === 'string' )
{
atlasData = JSON . parse ( atlasData ) ;
}
2013-05-23 14:45:04 +00:00
this . _queueSize ++ ;
2013-06-06 01:47:08 +00:00
this . _fileList [ key ] = { type : 'textureatlas' , key : key , url : textureURL , data : null , atlasURL : null , atlasData : atlasData , format : format , error : false , loaded : false } ;
2013-05-23 14:45:04 +00:00
this . _keys . push ( key ) ;
2013-04-18 13:16:18 +00:00
}
2013-05-23 02:08:57 +00:00
else if ( format == Loader . TEXTURE_ATLAS_XML_STARLING )
2013-04-12 16:19:56 +00:00
{
2013-05-23 02:08:57 +00:00
// An xml string or object has been given
if ( typeof atlasData === 'string' )
{
var xml ;
try
{
if ( window [ 'DOMParser' ] )
{
2013-05-23 14:45:04 +00:00
var domparser = new DOMParser ( ) ;
xml = domparser . parseFromString ( atlasData , "text/xml" ) ;
2013-05-23 02:08:57 +00:00
}
else
{
xml = new ActiveXObject ( "Microsoft.XMLDOM" ) ;
xml . async = 'false' ;
xml . loadXML ( atlasData ) ;
}
}
catch ( e )
{
xml = undefined ;
}
if ( ! xml || ! xml . documentElement || xml . getElementsByTagName ( "parsererror" ) . length )
{
throw new Error ( "Phaser.Loader. Invalid Texture Atlas XML given" ) ;
}
else
{
atlasData = xml ;
}
}
2013-05-23 14:45:04 +00:00
this . _queueSize ++ ;
this . _fileList [ key ] = { type : 'textureatlas' , key : key , url : textureURL , data : null , atlasURL : null , atlasData : atlasData , format : format , error : false , loaded : false } ;
this . _keys . push ( key ) ;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
2013-04-12 16:19:56 +00:00
}
}
}
2013-05-04 12:02:12 +00:00
/ * *
* Add a new audio file loading request .
* @param key { string } Unique asset key of the audio file .
2013-07-16 13:45:08 +00:00
* @param urls { Array } An array containing the URLs of the audio files , i . e . : [ 'jump.mp3' , 'jump.ogg' , 'jump.m4a' ]
* @param autoDecode { boolean } When using Web Audio the audio files can either be decoded at load time or run - time . They can 't be played until they are decoded, but this let' s you control when that happens . Decoding is a non - blocking async process .
2013-05-04 12:02:12 +00:00
* /
2013-07-16 13:45:08 +00:00
public audio ( key : string , urls : string [ ] , autoDecode : bool = true ) {
2013-04-18 13:16:18 +00:00
if ( this . checkKeyExists ( key ) === false )
{
2013-04-20 02:40:17 +00:00
this . _queueSize ++ ;
2013-07-16 13:45:08 +00:00
this . _fileList [ key ] = { type : 'audio' , key : key , url : urls , data : null , buffer : null , error : false , loaded : false , autoDecode : autoDecode } ;
2013-04-18 13:16:18 +00:00
this . _keys . push ( key ) ;
}
2013-04-12 16:19:56 +00:00
}
2013-05-04 12:02:12 +00:00
/ * *
* Add a new text file loading request .
* @param key { string } Unique asset key of the text file .
* @param url { string } URL of text file .
* /
2013-06-05 01:58:16 +00:00
public text ( key : string , url : string ) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if ( this . checkKeyExists ( key ) === false )
{
2013-04-20 02:40:17 +00:00
this . _queueSize ++ ;
2013-04-18 13:16:18 +00:00
this . _fileList [ key ] = { type : 'text' , key : key , url : url , data : null , error : false , loaded : false } ;
this . _keys . push ( key ) ;
}
2013-04-12 16:19:56 +00:00
}
2013-05-04 12:02:12 +00:00
/ * *
* Remove loading request of a file .
* @param key { string } Key of the file you want to remove .
* /
2013-04-18 13:16:18 +00:00
public removeFile ( key : string ) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
delete this . _fileList [ key ] ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Remove all file loading requests .
* /
2013-04-18 13:16:18 +00:00
public removeAll() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this . _fileList = { } ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Load assets .
* @param onFileLoadCallback { function } Called when each file loaded successfully .
* @param onCompleteCallback { function } Called when all assets completely loaded .
* /
2013-06-05 01:58:16 +00:00
public start ( onFileLoadCallback = null , onCompleteCallback = null ) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this . progress = 0 ;
this . hasLoaded = false ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this . _onComplete = onCompleteCallback ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if ( onCompleteCallback == null )
{
this . _onComplete = this . _game . onCreateCallback ;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this . _onFileLoad = onFileLoadCallback ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if ( this . _keys . length > 0 )
2013-04-12 16:19:56 +00:00
{
2013-04-18 13:16:18 +00:00
this . _progressChunk = 100 / this . _keys . length ;
this . loadFile ( ) ;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
else
{
2013-07-12 02:28:46 +00:00
this . progress = 100 ;
2013-04-18 13:16:18 +00:00
this . hasLoaded = true ;
this . _gameCreateComplete . call ( this . _game ) ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if ( this . _onComplete !== null )
{
this . _onComplete . call ( this . _game . callbackContext ) ;
}
}
2013-04-12 16:19:56 +00:00
}
2013-05-04 12:02:12 +00:00
/ * *
* Load files . Private method ONLY used by loader .
* /
2013-04-18 13:16:18 +00:00
private loadFile() {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var file = this . _fileList [ this . _keys . pop ( ) ] ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
// Image or Data?
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
switch ( file . type )
{
case 'image' :
case 'spritesheet' :
case 'textureatlas' :
file . data = new Image ( ) ;
file . data . name = file . key ;
file . data . onload = ( ) = > this . fileComplete ( file . key ) ;
file . data . onerror = ( ) = > this . fileError ( file . key ) ;
2013-05-22 23:01:58 +00:00
file . data . crossOrigin = this . crossOrigin ;
2013-04-18 13:16:18 +00:00
file . data . src = file . url ;
break ;
case 'audio' :
2013-07-16 13:45:08 +00:00
file . url = this . getAudioURL ( file . url ) ;
if ( file . url !== null )
{
// WebAudio or Audio Tag?
if ( this . _game . sound . usingWebAudio )
{
this . _xhr . open ( "GET" , file . url , true ) ;
this . _xhr . responseType = "arraybuffer" ;
this . _xhr . onload = ( ) = > this . fileComplete ( file . key ) ;
this . _xhr . onerror = ( ) = > this . fileError ( file . key ) ;
this . _xhr . send ( ) ;
}
else if ( this . _game . sound . usingAudioTag )
{
2013-07-16 22:32:47 +00:00
if ( this . _game . sound . touchLocked )
{
// If audio is locked we can't do this yet, so need to queue this load request somehow. Bum.
file . data = new Audio ( ) ;
file . data . name = file . key ;
file . data . preload = 'auto' ;
file . data . src = file . url ;
this . fileComplete ( file . key ) ;
}
else
{
file . data = new Audio ( ) ;
file . data . name = file . key ;
file . data . onerror = ( ) = > this . fileError ( file . key ) ;
file . data . preload = 'auto' ;
file . data . src = file . url ;
2013-08-05 02:43:20 +00:00
file . data . addEventListener ( 'canplaythrough' , Phaser . GAMES [ this . _game . id ] . load . fileComplete ( file . key ) , false ) ;
2013-07-16 22:32:47 +00:00
file . data . load ( ) ;
}
2013-07-16 13:45:08 +00:00
}
}
2013-04-18 13:16:18 +00:00
break ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
case 'text' :
this . _xhr . open ( "GET" , file . url , true ) ;
this . _xhr . responseType = "text" ;
this . _xhr . onload = ( ) = > this . fileComplete ( file . key ) ;
this . _xhr . onerror = ( ) = > this . fileError ( file . key ) ;
this . _xhr . send ( ) ;
break ;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-07-16 13:45:08 +00:00
private getAudioURL ( urls ) : string {
var extension : string ;
for ( var i = 0 ; i < urls . length ; i ++ )
{
extension = urls [ i ] . toLowerCase ( ) ;
extension = extension . substr ( ( Math . max ( 0 , extension . lastIndexOf ( "." ) ) || Infinity ) + 1 ) ;
if ( this . _game . device . canPlayAudio ( extension ) )
{
2013-07-19 01:59:23 +00:00
//console.log('getAudioURL', urls[i]);
//console.log(urls[i]);
2013-07-16 13:45:08 +00:00
return urls [ i ] ;
}
}
return null ;
}
2013-05-04 12:02:12 +00:00
/ * *
* Error occured when load a file .
* @param key { string } Key of the error loading file .
* /
2013-04-18 13:16:18 +00:00
private fileError ( key : string ) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this . _fileList [ key ] . loaded = true ;
this . _fileList [ key ] . error = true ;
2013-04-12 16:19:56 +00:00
2013-08-02 17:32:26 +00:00
throw new Error ( "Phaser.Loader error loading file: " + key ) ;
2013-04-18 13:16:18 +00:00
this . nextFile ( key , false ) ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
}
2013-04-12 16:19:56 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Called when a file is successfully loaded .
* @param key { string } Key of the successfully loaded file .
* /
2013-04-18 13:16:18 +00:00
private fileComplete ( key : string ) {
2013-04-12 16:19:56 +00:00
2013-08-05 02:43:20 +00:00
if ( ! this . _fileList [ key ] )
{
throw new Error ( 'Phaser.Loader fileComplete invalid key ' + key ) ;
return ;
}
2013-04-18 13:16:18 +00:00
this . _fileList [ key ] . loaded = true ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var file = this . _fileList [ key ] ;
var loadNext : bool = true ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
switch ( file . type )
{
case 'image' :
this . _game . cache . addImage ( file . key , file . url , file . data ) ;
break ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
case 'spritesheet' :
this . _game . cache . addSpriteSheet ( file . key , file . url , file . data , file . frameWidth , file . frameHeight , file . frameMax ) ;
break ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
case 'textureatlas' :
2013-05-23 02:08:57 +00:00
if ( file . atlasURL == null )
2013-04-18 13:16:18 +00:00
{
2013-05-23 02:08:57 +00:00
this . _game . cache . addTextureAtlas ( file . key , file . url , file . data , file . atlasData , file . format ) ;
2013-04-18 13:16:18 +00:00
}
else
{
2013-05-23 02:08:57 +00:00
// Load the JSON or XML before carrying on with the next file
2013-04-18 13:16:18 +00:00
loadNext = false ;
2013-05-23 02:08:57 +00:00
this . _xhr . open ( "GET" , file . atlasURL , true ) ;
2013-04-18 13:16:18 +00:00
this . _xhr . responseType = "text" ;
2013-05-23 02:08:57 +00:00
if ( file . format == Loader . TEXTURE_ATLAS_JSON_ARRAY )
{
this . _xhr . onload = ( ) = > this . jsonLoadComplete ( file . key ) ;
}
else if ( file . format == Loader . TEXTURE_ATLAS_XML_STARLING )
{
this . _xhr . onload = ( ) = > this . xmlLoadComplete ( file . key ) ;
}
this . _xhr . onerror = ( ) = > this . dataLoadError ( file . key ) ;
2013-04-18 13:16:18 +00:00
this . _xhr . send ( ) ;
}
break ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
case 'audio' :
2013-07-16 13:45:08 +00:00
if ( this . _game . sound . usingWebAudio )
{
file . data = this . _xhr . response ;
this . _game . cache . addSound ( file . key , file . url , 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
{
2013-08-05 02:43:20 +00:00
file . data . removeEventListener ( 'canplaythrough' , Phaser . GAMES [ this . _game . id ] . load . fileComplete ) ;
2013-07-16 13:45:08 +00:00
this . _game . cache . addSound ( file . key , file . url , file . data , false , true ) ;
}
2013-04-18 13:16:18 +00:00
break ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
case 'text' :
file . data = this . _xhr . response ;
this . _game . cache . addText ( file . key , file . url , file . data ) ;
break ;
}
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
if ( loadNext )
{
this . nextFile ( key , true ) ;
}
2013-04-12 16:19:56 +00:00
}
2013-05-04 12:02:12 +00:00
/ * *
* Successfully loaded a JSON file .
* @param key { string } Key of the loaded JSON file .
* /
2013-04-18 13:16:18 +00:00
private jsonLoadComplete ( key : string ) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var data = JSON . parse ( this . _xhr . response ) ;
2013-05-23 14:45:04 +00:00
var file = this . _fileList [ key ] ;
2013-04-12 16:19:56 +00:00
2013-06-03 11:03:34 +00:00
this . _game . cache . addTextureAtlas ( file . key , file . url , file . data , data , file . format ) ;
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
this . nextFile ( key , true ) ;
}
2013-04-12 16:19:56 +00:00
2013-05-04 12:02:12 +00:00
/ * *
* Error occured when load a JSON .
* @param key { string } Key of the error loading JSON file .
* /
2013-05-23 02:08:57 +00:00
private dataLoadError ( key : string ) {
2013-04-12 16:19:56 +00:00
2013-04-18 13:16:18 +00:00
var file = this . _fileList [ key ] ;
2013-05-23 14:45:04 +00:00
2013-04-18 13:16:18 +00:00
file . error = true ;
2013-05-23 14:45:04 +00:00
2013-08-02 17:32:26 +00:00
throw new Error ( "Phaser.Loader dataLoadError: " + key ) ;
2013-04-18 13:16:18 +00:00
this . nextFile ( key , true ) ;
2013-04-12 16:19:56 +00:00
}
2013-05-23 02:08:57 +00:00
private xmlLoadComplete ( key : string ) {
2013-05-23 14:45:04 +00:00
var atlasData = this . _xhr . response ;
2013-05-23 02:08:57 +00:00
var xml ;
try
{
if ( window [ 'DOMParser' ] )
{
2013-05-23 14:45:04 +00:00
var domparser = new DOMParser ( ) ;
xml = domparser . parseFromString ( atlasData , "text/xml" ) ;
2013-05-23 02:08:57 +00:00
}
else
{
xml = new ActiveXObject ( "Microsoft.XMLDOM" ) ;
xml . async = 'false' ;
xml . loadXML ( atlasData ) ;
}
}
catch ( e )
{
xml = undefined ;
}
if ( ! xml || ! xml . documentElement || xml . getElementsByTagName ( "parsererror" ) . length )
{
2013-05-25 03:21:24 +00:00
throw new Error ( "Phaser.Loader. Invalid XML given" ) ;
2013-05-23 02:08:57 +00:00
}
2013-05-23 14:45:04 +00:00
var file = this . _fileList [ key ] ;
this . _game . cache . addTextureAtlas ( file . key , file . url , file . data , xml , file . format ) ;
2013-05-23 02:08:57 +00:00
this . nextFile ( key , true ) ;
}
2013-05-04 12:02:12 +00:00
/ * *
* Handle loading next file .
* @param previousKey { string } Key of previous loaded asset .
* @param success { boolean } Whether the previous asset loaded successfully or not .
* /
2013-04-18 13:16:18 +00:00
private nextFile ( previousKey : string , success : bool ) {
this . progress = Math . round ( this . progress + this . _progressChunk ) ;
2013-05-04 12:02:12 +00:00
2013-05-17 05:49:43 +00:00
if ( this . progress > 100 )
2013-04-20 02:40:17 +00:00
{
2013-05-17 05:49:43 +00:00
this . progress = 100 ;
2013-04-20 02:40:17 +00:00
}
2013-04-18 13:16:18 +00:00
if ( this . _onFileLoad )
2013-04-12 16:19:56 +00:00
{
2013-04-18 13:16:18 +00:00
this . _onFileLoad . call ( this . _game . callbackContext , this . progress , previousKey , success ) ;
2013-04-12 16:19:56 +00:00
}
2013-04-18 13:16:18 +00:00
if ( this . _keys . length > 0 )
{
this . loadFile ( ) ;
}
else
{
this . hasLoaded = true ;
this . removeAll ( ) ;
this . _gameCreateComplete . call ( this . _game ) ;
if ( this . _onComplete !== null )
{
this . _onComplete . call ( this . _game . callbackContext ) ;
}
}
2013-04-12 16:19:56 +00:00
}
2013-05-04 12:02:12 +00:00
/ * *
* Check whether asset exists with a specific key .
* @param key { string } Key of the asset you want to check .
* @return { boolean } Return true if exists , otherwise return false .
* /
2013-04-20 02:40:17 +00:00
private checkKeyExists ( key : string ) : bool {
if ( this . _fileList [ key ] )
{
return true ;
}
else
{
return false ;
}
}
2013-04-12 16:19:56 +00:00
}
}