Phaser.Cache = function (game){ this.game = game; this._canvases = { } ; this._images = { } ; this._textures = { } ; this._sounds = { } ; this._text = { } ; this._tilemaps = { } ; this._tilesets = { } ; this.addDefaultImage(); this.onSoundUnlock = new Phaser.Signal(); } ; Phaser.Cache.prototype = { addCanvas: function (key, canvas, context){ this._canvases[key] = { canvas: canvas, context: context} ; } , addRenderTexture: function (key, texture){ var frame = new Phaser.Frame(0, 0, 0, texture.width, texture.height, '', ''); this._textures[key] = { texture: texture, frame: frame} ; } , addSpriteSheet: function (key, url, data, frameWidth, frameHeight, frameMax){ this._images[key] = { url: url, data: data, spriteSheet: true , frameWidth: frameWidth, frameHeight: frameHeight} ; PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data); PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]); this._images[key].frameData = Phaser.AnimationParser.spriteSheet(this.game, key, frameWidth, frameHeight, frameMax); } , addTileset: function (key, url, data, tileWidth, tileHeight, tileMax, tileMargin, tileSpacing){ this._tilesets[key] = { url: url, data: data, tileWidth: tileWidth, tileHeight: tileHeight, tileMargin: tileMargin, tileSpacing: tileSpacing} ; PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data); PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]); this._tilesets[key].tileData = Phaser.TilemapParser.tileset(this.game, key, tileWidth, tileHeight, tileMax, tileMargin, tileSpacing); } , addTilemap: function (key, url, mapData, format){ this._tilemaps[key] = { url: url, data: mapData, format: format} ; this._tilemaps[key].layers = Phaser.TilemapParser.parse(this.game, mapData, format); } , addTextureAtlas: function (key, url, data, atlasData, format){ this._images[key] = { url: url, data: data, spriteSheet: true } ; PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data); PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]); if (format == Phaser.Loader.TEXTURE_ATLAS_JSON_ARRAY) { this._images[key].frameData = Phaser.AnimationParser.JSONData(this.game, atlasData, key); } else if (format == Phaser.Loader.TEXTURE_ATLAS_JSON_HASH) { this._images[key].frameData = Phaser.AnimationParser.JSONDataHash(this.game, atlasData, key); } else if (format == Phaser.Loader.TEXTURE_ATLAS_XML_STARLING) { this._images[key].frameData = Phaser.AnimationParser.XMLData(this.game, atlasData, key); } } , addBitmapFont: function (key, url, data, xmlData){ this._images[key] = { url: url, data: data, spriteSheet: true } ; PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data); PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]); Phaser.LoaderParser.bitmapFont(this.game, xmlData, key); } , addDefaultImage: function (){ var img = new Image(); _AN_Write_src('src', img, false , "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAJ9JREFUeNq01ssOwyAMRFG46v//Mt1ESmgh+DFmE2GPOBARKb2NVjo+17PXLD8a1+pl5+A+wSgFygymWYHBb0FtsKhJDdZlncG2IzJ4ayoMDv20wTmSMzClEgbWYNTAkQ0Z+OJ+A/eWnAaR9+oxCF4Os0H8htsMUp+pwcgBBiMNnAwF8GqIgL2hAzaGFFgZauDPKABmowZ4GL369/0rwACp2yA/ttmvsQAAAABJRU5ErkJggg=="); this._images.__default = { url: null , data: img, spriteSheet: false } ; this._images.__default.frame = new Phaser.Frame(0, 0, 0, 32, 32, '', ''); PIXI.BaseTextureCache.__default = new PIXI.BaseTexture(img); PIXI.TextureCache.__default = new PIXI.Texture(PIXI.BaseTextureCache.__default); } , addText: function (key, url, data){ this._text[key] = { url: url, data: data} ; } , addImage: function (key, url, data){ this._images[key] = { url: url, data: data, spriteSheet: false } ; this._images[key].frame = new Phaser.Frame(0, 0, 0, data.width, data.height, '', ''); PIXI.BaseTextureCache[key] = new PIXI.BaseTexture(data); PIXI.TextureCache[key] = new PIXI.Texture(PIXI.BaseTextureCache[key]); } , addSound: function (key, url, data, webAudio, audioTag){ webAudio = webAudio || true ; audioTag = audioTag || false ; var locked = this.game.sound.touchLocked; var decoded = false ; if (audioTag) { decoded = true ; } this._sounds[key] = { url: url, data: data, isDecoding: false , decoded: decoded, webAudio: webAudio, audioTag: audioTag} ; } , reloadSound: function (key){ var _this = this; if (this._sounds[key]) { _AN_Write_src('src', this._sounds[key].data, false , _AN_Read_url('url', this._sounds[key])); this._sounds[key].data.addEventListener('canplaythrough', function (){ return _this.reloadSoundComplete(key); } , false ); _AN_Call_load('load', this._sounds[key].data); } } , reloadSoundComplete: function (key){ if (this._sounds[key]) { this._sounds[key].locked = false ; this.onSoundUnlock.dispatch(key); } } , updateSound: function (key, property, value){ if (this._sounds[key]) { this._sounds[key][property] = value; } } , decodedSound: function (key, data){ this._sounds[key].data = data; this._sounds[key].decoded = true ; this._sounds[key].isDecoding = false ; } , getCanvas: function (key){ if (this._canvases[key]) { return this._canvases[key].canvas; } return null ; } , checkImageKey: function (key){ if (this._images[key]) { return true ; } return false ; } , getImage: function (key){ if (this._images[key]) { return this._images[key].data; } return null ; } , getTilesetImage: function (key){ if (this._tilesets[key]) { return this._tilesets[key].data; } return null ; } , getTileset: function (key){ if (this._tilesets[key]) { return this._tilesets[key].tileData; } return null ; } , getTilemapData: function (key){ if (this._tilemaps[key]) { return this._tilemaps[key]; } return null ; } , getFrameData: function (key){ if (this._images[key] && this._images[key].frameData) { return this._images[key].frameData; } return null ; } , getFrameByIndex: function (key, frame){ if (this._images[key] && this._images[key].frameData) { return this._images[key].frameData.getFrame(frame); } return null ; } , getFrameByName: function (key, frame){ if (this._images[key] && this._images[key].frameData) { return this._images[key].frameData.getFrameByName(frame); } return null ; } , getFrame: function (key){ if (this._images[key] && this._images[key].spriteSheet == false ) { return this._images[key].frame; } return null ; } , getTextureFrame: function (key){ if (this._textures[key]) { return this._textures[key].frame; } return null ; } , getTexture: function (key){ if (this._textures[key]) { return this._textures[key]; } return null ; } , getSound: function (key){ if (this._sounds[key]) { return this._sounds[key]; } return null ; } , getSoundData: function (key){ if (this._sounds[key]) { return this._sounds[key].data; } return null ; } , isSoundDecoded: function (key){ if (this._sounds[key]) { return this._sounds[key].decoded; } } , isSoundReady: function (key){ return (this._sounds[key] && this._sounds[key].decoded && this.game.sound.touchLocked == false ); } , isSpriteSheet: function (key){ if (this._images[key]) { return this._images[key].spriteSheet; } return false ; } , getText: function (key){ if (this._text[key]) { return this._text[key].data; } return null ; } , getKeys: function (array){ var output = [] ; for (var item in array){ if (item !== '__default') { output.push(item); } } return output; } , getImageKeys: function (){ return this.getKeys(this._images); } , getSoundKeys: function (){ return this.getKeys(this._sounds); } , getTextKeys: function (){ return this.getKeys(this._text); } , removeCanvas: function (key){ delete this._canvases[key]; } , removeImage: function (key){ delete this._images[key]; } , removeSound: function (key){ delete this._sounds[key]; } , removeText: function (key){ delete this._text[key]; } , destroy: function (){ for (var item in this._canvases){ delete this._canvases[item.key]; } for (var item in this._images){ delete this._images[item.key]; } for (var item in this._sounds){ delete this._sounds[item.key]; } for (var item in this._text){ delete this._text[item.key]; } } } ;