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 - loader/Cache.js, line 15
Members
-
<static, constant> BINARY :number
-
- Source - loader/Cache.js, line 192
-
<static, constant> BITMAPDATA :number
-
- Source - loader/Cache.js, line 198
-
<static, constant> BITMAPFONT :number
-
- Source - loader/Cache.js, line 204
-
<static, constant> CANVAS :number
-
- Source - loader/Cache.js, line 150
-
<static, constant> IMAGE :number
-
- Source - loader/Cache.js, line 156
-
<static, constant> JSON :number
-
- Source - loader/Cache.js, line 210
-
<static, constant> PHYSICS :number
-
- Source - loader/Cache.js, line 180
-
<static, constant> SOUND :number
-
- Source - loader/Cache.js, line 168
-
<static, constant> TEXT :number
-
- Source - loader/Cache.js, line 174
-
<static, constant> TEXTURE :number
-
- Source - loader/Cache.js, line 162
-
<static, constant> TILEMAP :number
-
- Source - loader/Cache.js, line 186
-
<static, constant> XML :number
-
- Source - loader/Cache.js, line 216
-
autoResolveURL :boolean
-
Automatically resolve resource URLs to absolute paths for use with the Cache.getURL method.
- Source - loader/Cache.js, line 25
-
game :Phaser.Game
-
Local reference to game.
- Source - 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 - loader/Cache.js, line 123
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 - 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. Ifnull
is supplied then no FrameData will be created.Returns:
The BitmapData object to be addded to the cache.
- Source - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - loader/Cache.js, line 607
-
destroy()
-
Clears the cache. Removes every local cache object reference.
- Source - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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.
- Source - 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 - 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 - 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 - 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.
- Source - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - 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 - loader/Cache.js, line 592
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.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:
The cached object.