Navigation
These archived docs are for Phaser 2.3.0 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 192)

[static] BITMAPDATA : number

Source code: loader/Cache.js (Line 198)

[static] BITMAPFONT : number

Source code: loader/Cache.js (Line 204)

[static] CANVAS : number

Source code: loader/Cache.js (Line 150)

[static] IMAGE : number

Source code: loader/Cache.js (Line 156)

[static] JSON : number

Source code: loader/Cache.js (Line 210)

[static] PHYSICS : number

Source code: loader/Cache.js (Line 180)

[static] SOUND : number

Source code: loader/Cache.js (Line 168)

[static] TEXT : number

Source code: loader/Cache.js (Line 174)

[static] TEXTURE : number

Source code: loader/Cache.js (Line 162)

[static] TILEMAP : number

Source code: loader/Cache.js (Line 186)

[static] XML : number

Source code: loader/Cache.js (Line 216)

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 234)

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 247)

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 364)

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 220)

<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 407)

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. If an image already exists in the cache with the same key then it is removed and destroyed, and the new image inserted in its place.

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 497)

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 465)

<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 428)

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 390)

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 272)

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 526)

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 287)

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 449)

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 330)

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 313)

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 481)

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 857)

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 870)

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 883)

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 766)

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 779)

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 896)

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 747)

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 831)

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 805)

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 818)

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 792)

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 844)

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 922)

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 909)

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 607)

destroy()

Clears the cache. Removes every local cache object reference.

Source code: loader/Cache.js (Line 1583)

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 1285)

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 643)

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 664)

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 622)

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 1055)

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 1021)

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 1038)

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 1204)

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 985)

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 943)

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 1243)

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 1348)

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 685)

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 1089)

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 1133)

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 1154)

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 1222)

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 1110)

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 1072)

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 964)

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 1306)

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 1331)

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 1264)

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 1175)

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 1191)

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 554)

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 576)

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 1519)

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 1529)

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 1539)

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 1429)

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 1439)

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 1479)

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 1499)

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 1459)

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 1469)

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 1509)

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 1489)

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 1005)

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 592)