Navigation
These archived docs are for Phaser 2.2.2 Phaser 3 docs can be found on newdocs.phaser.io.
Phaser CE docs can be found on the Phaser CE Documentation site.

Constructor

Phaser. Cache

new Cache(game)

A game only has one instance of a Cache and it is used to store all externally loaded assets such as images, sounds and data files as a result of Loader calls. Cached items use string based keys for look-up.

Parameters
Name Type Description
game Phaser.Game

A reference to the currently running game.

Source code: loader/Cache.js, line 15

Public Properties

[static] BINARY : number

Source code: loader/Cache.js, line 191

[static] BITMAPDATA : number

Source code: loader/Cache.js, line 197

[static] BITMAPFONT : number

Source code: loader/Cache.js, line 203

[static] CANVAS : number

Source code: loader/Cache.js, line 149

[static] IMAGE : number

Source code: loader/Cache.js, line 155

[static] JSON : number

Source code: loader/Cache.js, line 209

[static] PHYSICS : number

Source code: loader/Cache.js, line 179

[static] SOUND : number

Source code: loader/Cache.js, line 167

[static] TEXT : number

Source code: loader/Cache.js, line 173

[static] TEXTURE : number

Source code: loader/Cache.js, line 161

[static] TILEMAP : number

Source code: loader/Cache.js, line 185

[static] XML : number

Source code: loader/Cache.js, line 215

_cacheMap :array

Const to cache object look-up array.

Source code: loader/Cache.js, line 128

autoResolveURL : boolean

Automatically resolve resource URLs to absolute paths for use with the Cache.getURL method.

Source code: loader/Cache.js, line 25

game : Phaser.Game

Local reference to game.

Source code: loader/Cache.js, line 20

onSoundUnlock : Phaser.Signal

This event is dispatched when the sound system is unlocked via a touch event on cellular devices.

Source code: loader/Cache.js, line 123

Public Methods

addBinary(key, binaryData)

Add a binary object in to the cache.

Parameters
Name Type Description
key string

Asset key for this binary data.

binaryData object

The binary object to be addded to the cache.

Source code: loader/Cache.js, line 233

addBitmapData(key, bitmapData, frameData) → {Phaser.BitmapData}

Add a BitmapData object to the cache.

Parameters
Name Type Argument Default Description
key string

Asset key for this BitmapData.

bitmapData Phaser.BitmapData

The BitmapData object to be addded to the cache.

frameData Phaser.FrameData | null <optional>
(auto create)

Optional FrameData set associated with the given BitmapData. If not specified (or undefined) a new FrameData object is created containing the Bitmap's Frame. If null is supplied then no FrameData will be created.

Returns

The BitmapData object to be addded to the cache.

Source code: loader/Cache.js, line 246

addBitmapFont(key, url, data, xmlData, xSpacing, ySpacing)

Add a new Bitmap Font to the Cache.

Parameters
Name Type Argument Default Description
key string

The unique key by which you will reference this object.

url string

URL of this font xml file.

data object

Extra font data.

xmlData object

Texture atlas frames data.

xSpacing number <optional>
0

If you'd like to add additional horizontal spacing between the characters then set the pixel value here.

ySpacing number <optional>
0

If you'd like to add additional vertical spacing between the lines then set the pixel value here.

Source code: loader/Cache.js, line 363

addCanvas(key, canvas, context)

Add a new canvas object in to the cache.

Parameters
Name Type Description
key string

Asset key for this canvas.

canvas HTMLCanvasElement

Canvas DOM element.

context CanvasRenderingContext2D

Render context of this canvas.

Source code: loader/Cache.js, line 219

<internal> addDefaultImage()

Adds a default image to be used in special cases such as WebGL Filters. Is mapped to the key __default.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Cache.js, line 406

addImage(key, url, data)

Adds an Image file into the Cache. The file must have already been loaded, typically via Phaser.Loader, but can also have been loaded into the DOM.

Parameters
Name Type Description
key string

The unique key by which you will reference this object.

url string

URL of this image file.

data object

Extra image data.

Source code: loader/Cache.js, line 496

addJSON(key, url, data)

Add a new json object into the cache.

Parameters
Name Type Description
key string

Asset key for the json data.

url string

URL of this json data file.

data object

Extra json data.

Source code: loader/Cache.js, line 464

<internal> addMissingImage()

Adds an image to be used when a key is wrong / missing. Is mapped to the key __missing.

Internal:
  • This member is internal (protected) and may be modified or removed in the future.
Source code: loader/Cache.js, line 427

addPhysicsData(key, url, JSONData, format)

Add a new physics data object to the Cache.

Parameters
Name Type Description
key string

The unique key by which you will reference this object.

url string

URL of the physics json data.

JSONData object

The physics data object (a JSON file).

format number

The format of the physics data.

Source code: loader/Cache.js, line 389

addRenderTexture(key, texture)

Add a new Phaser.RenderTexture in to the cache.

Parameters
Name Type Description
key string

The unique key by which you will reference this object.

texture Phaser.RenderTexture

The texture to use as the base of the RenderTexture.

Source code: loader/Cache.js, line 271

addSound(key, url, data, webAudio, audioTag)

Adds a Sound file into the Cache. The file must have already been loaded, typically via Phaser.Loader.

Parameters
Name Type Description
key string

Asset key for the sound.

url string

URL of this sound file.

data object

Extra sound data.

webAudio boolean

True if the file is using web audio.

audioTag boolean

True if the file is using legacy HTML audio.

Source code: loader/Cache.js, line 519

addSpriteSheet(key, url, data, frameWidth, frameHeight, frameMax, margin, spacing)

Add a new sprite sheet in to the cache.

Parameters
Name Type Argument Default Description
key string

The unique key by which you will reference this object.

url string

URL of this sprite sheet file.

data object

Extra sprite sheet data.

frameWidth number

Width of the sprite sheet.

frameHeight number

Height of the sprite sheet.

frameMax number <optional>
-1

How many frames stored in the sprite sheet. If -1 then it divides the whole sheet evenly.

margin number <optional>
0

If the frames have been drawn with a margin, specify the amount here.

spacing number <optional>
0

If the frames have been drawn with spacing between them, specify the amount here.

Source code: loader/Cache.js, line 286

addText(key, url, data)

Add a new text data.

Parameters
Name Type Description
key string

Asset key for the text data.

url string

URL of this text data file.

data object

Extra text data.

Source code: loader/Cache.js, line 448

addTextureAtlas(key, url, data, atlasData, format)

Add a new texture atlas to the Cache.

Parameters
Name Type Description
key string

The unique key by which you will reference this object.

url string

URL of this texture atlas file.

data object

Extra texture atlas data.

atlasData object

Texture atlas frames data.

format number

The format of the texture atlas.

Source code: loader/Cache.js, line 329

addTilemap(key, url, mapData, format)

Add a new tilemap to the Cache.

Parameters
Name Type Description
key string

The unique key by which you will reference this object.

url string

URL of the tilemap image.

mapData object

The tilemap data object (either a CSV or JSON file).

format number

The format of the tilemap data.

Source code: loader/Cache.js, line 312

addXML(key, url, data)

Add a new xml object into the cache.

Parameters
Name Type Description
key string

Asset key for the xml file.

url string

URL of this xml file.

data object

Extra text data.

Source code: loader/Cache.js, line 480

checkBinaryKey(key) → {boolean}

Checks if the given key exists in the Binary Cache.

Parameters
Name Type Description
key string

Asset key of the binary file to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 850

checkBitmapDataKey(key) → {boolean}

Checks if the given key exists in the BitmapData Cache.

Parameters
Name Type Description
key string

Asset key of the BitmapData to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 863

checkBitmapFontKey(key) → {boolean}

Checks if the given key exists in the BitmapFont Cache.

Parameters
Name Type Description
key string

Asset key of the BitmapFont to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 876

checkCanvasKey(key) → {boolean}

Checks if the given key exists in the Canvas Cache.

Parameters
Name Type Description
key string

Asset key of the canvas to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 759

checkImageKey(key) → {boolean}

Checks if the given key exists in the Image Cache. Note that this also includes Texture Atlases, Sprite Sheets and Retro Fonts.

Parameters
Name Type Description
key string

Asset key of the image to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 772

checkJSONKey(key) → {boolean}

Checks if the given key exists in the JSON Cache.

Parameters
Name Type Description
key string

Asset key of the JSON file to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 889

checkKey(type, key) → {boolean}

Checks if a key for the given cache object type exists.

Parameters
Name Type Description
type number

The Cache type to check against. I.e. Phaser.Cache.CANVAS, Phaser.Cache.IMAGE, Phaser.Cache.JSON, etc.

key string

Asset key of the image to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 740

checkPhysicsKey(key) → {boolean}

Checks if the given key exists in the Physics Cache.

Parameters
Name Type Description
key string

Asset key of the physics data file to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 824

checkSoundKey(key) → {boolean}

Checks if the given key exists in the Sound Cache.

Parameters
Name Type Description
key string

Asset key of the sound file to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 798

checkTextKey(key) → {boolean}

Checks if the given key exists in the Text Cache.

Parameters
Name Type Description
key string

Asset key of the text file to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 811

checkTextureKey(key) → {boolean}

Checks if the given key exists in the Texture Cache.

Parameters
Name Type Description
key string

Asset key of the image to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 785

checkTilemapKey(key) → {boolean}

Checks if the given key exists in the Tilemap Cache.

Parameters
Name Type Description
key string

Asset key of the Tilemap to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 837

checkURL(url) → {boolean}

Checks if the given URL has been loaded into the Cache. This method will only work if Cache.autoResolveURL was set to true before any preloading took place. The method will make a DOM src call to the URL given, so please be aware of this for certain file types, such as Sound files on Firefox which may cause double-load instances.

Parameters
Name Type Description
url string

The url to check for in the cache.

Returns
boolean -

True if the url exists, otherwise false.

Source code: loader/Cache.js, line 915

checkXMLKey(key) → {boolean}

Checks if the given key exists in the XML Cache.

Parameters
Name Type Description
key string

Asset key of the XML file to check is in the Cache.

Returns
boolean -

True if the key exists, otherwise false.

Source code: loader/Cache.js, line 902

decodedSound(key, data)

Add a new decoded sound.

Parameters
Name Type Description
key string

Asset key for the sound.

data object

Extra sound data.

Source code: loader/Cache.js, line 600

destroy()

Clears the cache. Removes every local cache object reference.

Source code: loader/Cache.js, line 1576

getBinary(key) → {object}

Get binary data by key.

Parameters
Name Type Description
key string

Asset key of the binary data object to retrieve from the Cache.

Returns
object -

The binary data object.

Source code: loader/Cache.js, line 1278

getBitmapData(key) → {Phaser.BitmapData}

Get a BitmapData object from the cache by its key.

Parameters
Name Type Description
key string

Asset key of the BitmapData object to retrieve from the Cache.

Returns

The requested BitmapData object if found, or null if not.

Source code: loader/Cache.js, line 636

getBitmapFont(key) → {Phaser.BitmapFont}

Get a BitmapFont object from the cache by its key.

Parameters
Name Type Description
key string

Asset key of the BitmapFont object to retrieve from the Cache.

Returns
Phaser.BitmapFont -

The requested BitmapFont object if found, or null if not.

Source code: loader/Cache.js, line 657

getCanvas(key) → {object}

Get a canvas object from the cache by its key.

Parameters
Name Type Description
key string

Asset key of the canvas to retrieve from the Cache.

Returns
object -

The canvas object.

Source code: loader/Cache.js, line 615

getFrame(key) → {Phaser.Frame}

Get a single frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.

Parameters
Name Type Description
key string

Asset key of the frame data to retrieve from the Cache.

Returns

The frame data.

Source code: loader/Cache.js, line 1048

getFrameByIndex(key) → {Phaser.Frame}

Get a single frame out of a frameData set by key.

Parameters
Name Type Description
key string

Asset key of the frame data to retrieve from the Cache.

Returns

The frame object.

Source code: loader/Cache.js, line 1014

getFrameByName(key) → {Phaser.Frame}

Get a single frame out of a frameData set by key.

Parameters
Name Type Description
key string

Asset key of the frame data to retrieve from the Cache.

Returns

The frame object.

Source code: loader/Cache.js, line 1031

getFrameCount(key) → {number}

Get the number of frames in this image.

Parameters
Name Type Description
key string

Asset key of the image you want.

Returns
number -

Then number of frames. 0 if the image is not found.

Source code: loader/Cache.js, line 1197

getFrameData(key, map) → {Phaser.FrameData}

Get frame data by key.

Parameters
Name Type Argument Default Description
key string

Asset key of the frame data to retrieve from the Cache.

map string <optional>
Phaser.Cache.IMAGE

The asset map to get the frameData from, for example Phaser.Cache.IMAGE.

Returns

The frame data.

Source code: loader/Cache.js, line 978

getImage(key) → {Image}

Gets an image by its key. Note that this returns a DOM Image object, not a Phaser object.

Parameters
Name Type Description
key string

Asset key of the image to retrieve from the Cache.

Returns
Image -

The Image object if found in the Cache, otherwise null.

Source code: loader/Cache.js, line 936

getJSON(key) → {object}

Get a JSON object by key from the cache.

Parameters
Name Type Description
key string

Asset key of the json object to retrieve from the Cache.

Returns
object -

The JSON object.

Source code: loader/Cache.js, line 1236

getKeys(type) → {Array}

Gets all keys used by the Cache for the given data type.

Parameters
Name Type Argument Default Description
type number <optional>
Phaser.Cache.IMAGE

The type of Cache keys you wish to get. Can be Cache.CANVAS, Cache.IMAGE, Cache.SOUND, etc.

Returns
Array -

The array of item keys.

Source code: loader/Cache.js, line 1341

getPhysicsData(key, object, fixtureKey) → {object}

Get a physics data object from the cache by its key. You can get either the entire data set, a single object or a single fixture of an object from it.

Parameters
Name Type Argument Default Description
key string

Asset key of the physics data object to retrieve from the Cache.

object string <optional>
null

If specified it will return just the physics object that is part of the given key, if null it will return them all.

fixtureKey string

Fixture key of fixture inside an object. This key can be set per fixture with the Phaser Exporter.

Returns
object -

The requested physics object data if found.

Source code: loader/Cache.js, line 678

getRenderTexture(key) → {Phaser.RenderTexture}

Get a RenderTexture by key.

Parameters
Name Type Description
key string

Asset key of the RenderTexture to retrieve from the Cache.

Returns

The RenderTexture object.

Source code: loader/Cache.js, line 1082

getSound(key) → {Phaser.Sound}

Get sound by key.

Parameters
Name Type Description
key string

Asset key of the sound to retrieve from the Cache.

Returns

The sound object.

Source code: loader/Cache.js, line 1126

getSoundData(key) → {object}

Get sound data by key.

Parameters
Name Type Description
key string

Asset key of the sound to retrieve from the Cache.

Returns
object -

The sound data.

Source code: loader/Cache.js, line 1147

getText(key) → {object}

Get text data by key.

Parameters
Name Type Description
key string

Asset key of the text data to retrieve from the Cache.

Returns
object -

The text data.

Source code: loader/Cache.js, line 1215

getTexture(key) → {Phaser.RenderTexture}

This method is deprecated and should not be used. It may be removed in the future.

DEPRECATED: Please use Cache.getRenderTexture instead. This method will be removed in Phaser 2.2.0.

Get a RenderTexture by key.

Parameters
Name Type Description
key string

Asset key of the RenderTexture to retrieve from the Cache.

Returns

The RenderTexture object.

Deprecated:
  • Please use Cache.getRenderTexture instead. This method will be removed in Phaser 2.2.0.
Source code: loader/Cache.js, line 1103

getTextureFrame(key) → {Phaser.Frame}

Get a single texture frame by key. You'd only do this to get the default Frame created for a non-atlas/spritesheet image.

Parameters
Name Type Description
key string

Asset key of the frame to retrieve from the Cache.

Returns

The frame data.

Source code: loader/Cache.js, line 1065

getTilemapData(key) → {object}

Get tilemap data by key.

Parameters
Name Type Description
key string

Asset key of the tilemap data to retrieve from the Cache.

Returns
object -

The raw tilemap data in CSV or JSON format.

Source code: loader/Cache.js, line 957

getURL(url) → {object}

Get a cached object by the URL. This only returns a value if you set Cache.autoResolveURL to true before starting the preload of any assets. Be aware that every call to this function makes a DOM src query, so use carefully and double-check for implications in your target browsers/devices.

Parameters
Name Type Description
url string

The url for the object loaded to get from the cache.

Returns
object -

The cached object.

Source code: loader/Cache.js, line 1299

getUrl(url) → {object}

This method is deprecated and should not be used. It may be removed in the future.

DEPRECATED: Please use Cache.getURL instead. Get a cached object by the URL. This only returns a value if you set Cache.autoResolveURL to true before starting the preload of any assets. Be aware that every call to this function makes a DOM src query, so use carefully and double-check for implications in your target browsers/devices.

Parameters
Name Type Description
url string

The url for the object loaded to get from the cache.

Returns
object -

The cached object.

Deprecated:
  • Please use Cache.getURL instead.
Source code: loader/Cache.js, line 1324

getXML(key) → {object}

Get a XML object by key from the cache.

Parameters
Name Type Description
key string

Asset key of the XML object to retrieve from the Cache.

Returns
object -

The XML object.

Source code: loader/Cache.js, line 1257

isSoundDecoded(key) → {boolean}

Check if the given sound has finished decoding.

Parameters
Name Type Description
key string

Asset key of the sound in the Cache.

Returns
boolean -

The decoded state of the Sound object.

Source code: loader/Cache.js, line 1168

isSoundReady(key) → {boolean}

Check if the given sound is ready for playback. A sound is considered ready when it has finished decoding and the device is no longer touch locked.

Parameters
Name Type Description
key string

Asset key of the sound in the Cache.

Returns
boolean -

True if the sound is decoded and the device is not touch locked.

Source code: loader/Cache.js, line 1184

reloadSound(key)

Reload a Sound file from the server.

Parameters
Name Type Description
key string

Asset key for the sound.

Source code: loader/Cache.js, line 547

reloadSoundComplete(key)

Fires the onSoundUnlock event when the sound has completed reloading.

Parameters
Name Type Description
key string

Asset key for the sound.

Source code: loader/Cache.js, line 569

removeBinary(key)

Removes a binary file from the cache.

Parameters
Name Type Description
key string

Key of the asset you want to remove.

Source code: loader/Cache.js, line 1512

removeBitmapData(key)

Removes a bitmap data from the cache.

Parameters
Name Type Description
key string

Key of the asset you want to remove.

Source code: loader/Cache.js, line 1522

removeBitmapFont(key)

Removes a bitmap font from the cache.

Parameters
Name Type Description
key string

Key of the asset you want to remove.

Source code: loader/Cache.js, line 1532

removeCanvas(key)

Removes a canvas from the cache.

Parameters
Name Type Description
key string

Key of the asset you want to remove.

Source code: loader/Cache.js, line 1422

removeImage(key, removeFromPixi)

Removes an image from the cache and optionally from the Pixi.BaseTextureCache as well.

Parameters
Name Type Argument Default Description
key string

Key of the asset you want to remove.

removeFromPixi boolean <optional>
true

Should this image also be removed from the Pixi BaseTextureCache?

Source code: loader/Cache.js, line 1432

removeJSON(key)

Removes a json object from the cache.

Parameters
Name Type Description
key string

Key of the asset you want to remove.

Source code: loader/Cache.js, line 1472

removePhysics(key)

Removes a physics data file from the cache.

Parameters
Name Type Description
key string

Key of the asset you want to remove.

Source code: loader/Cache.js, line 1492

removeSound(key)

Removes a sound from the cache.

Parameters
Name Type Description
key string

Key of the asset you want to remove.

Source code: loader/Cache.js, line 1452

removeText(key)

Removes a text from the cache.

Parameters
Name Type Description
key string

Key of the asset you want to remove.

Source code: loader/Cache.js, line 1462

removeTilemap(key)

Removes a tilemap from the cache.

Parameters
Name Type Description
key string

Key of the asset you want to remove.

Source code: loader/Cache.js, line 1502

removeXML(key)

Removes a xml object from the cache.

Parameters
Name Type Description
key string

Key of the asset you want to remove.

Source code: loader/Cache.js, line 1482

updateFrameData(key, frameData)

Replaces a set of frameData with a new Phaser.FrameData object.

Parameters
Name Type Description
key string

The unique key by which you will reference this object.

frameData number

The new FrameData.

Source code: loader/Cache.js, line 998

updateSound(key)

Updates the sound object in the cache.

Parameters
Name Type Description
key string

Asset key for the sound.

Source code: loader/Cache.js, line 585