Skip to content

Commit da75a22

Browse files
committed
Cache.checkKey added - allows you to pass in a Cache type and a key and return a boolean.
Cache.checkCanvasKey(key) - Check if a Canvas key exists in the cache (thanks to @delta11 for the proposal) Cache.checkTextureKey(key) - Check if a Texture key exists in the cache (thanks to @delta11 for the proposal) Cache.checkSoundKey(key) - Check if a Sound key exists in the cache (thanks to @delta11 for the proposal) Cache.checkTextKey(key) - Check if a Text key exists in the cache (thanks to @delta11 for the proposal) Cache.checkPhysicsKey(key) - Check if a Physics key exists in the cache (thanks to @delta11 for the proposal) Cache.checkTilemapKey(key) - Check if a Tilemap key exists in the cache (thanks to @delta11 for the proposal) Cache.checkBinaryKey(key) - Check if a Binary key exists in the cache (thanks to @delta11 for the proposal) Cache.checkBitmapDataKey(key) - Check if a BitmapData key exists in the cache (thanks to @delta11 for the proposal) Cache.checkBitmapFontKey(key) - Check if a BitmapFont key exists in the cache (thanks to @delta11 for the proposal) Cache.checkJSONKey(key) - Check if a JSON key exists in the cache (thanks to @delta11 for the proposal)
1 parent cfadaf3 commit da75a22

3 files changed

Lines changed: 187 additions & 4 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ Version 2.0.5 - "Tanchico" - in development
9999
* Mouse.stopOnGameOut boolean controls if Pointer.stop will be called if the mouse leaves the game canvas (defaults to false)
100100
* Tilemap.searchTileIndex allows you to search for the first tile matching the given index, with optional skip and reverse parameters.
101101
* Tilemap.layer is a getter/setter to the current layer object (which can be changed with Tilemap.setLayer)
102+
* Cache.checkKey added - allows you to pass in a Cache type and a key and return a boolean.
103+
* Cache.checkCanvasKey(key) - Check if a Canvas key exists in the cache (thanks to @delta11 for the proposal)
104+
* Cache.checkTextureKey(key) - Check if a Texture key exists in the cache (thanks to @delta11 for the proposal)
105+
* Cache.checkSoundKey(key) - Check if a Sound key exists in the cache (thanks to @delta11 for the proposal)
106+
* Cache.checkTextKey(key) - Check if a Text key exists in the cache (thanks to @delta11 for the proposal)
107+
* Cache.checkPhysicsKey(key) - Check if a Physics key exists in the cache (thanks to @delta11 for the proposal)
108+
* Cache.checkTilemapKey(key) - Check if a Tilemap key exists in the cache (thanks to @delta11 for the proposal)
109+
* Cache.checkBinaryKey(key) - Check if a Binary key exists in the cache (thanks to @delta11 for the proposal)
110+
* Cache.checkBitmapDataKey(key) - Check if a BitmapData key exists in the cache (thanks to @delta11 for the proposal)
111+
* Cache.checkBitmapFontKey(key) - Check if a BitmapFont key exists in the cache (thanks to @delta11 for the proposal)
112+
* Cache.checkJSONKey(key) - Check if a JSON key exists in the cache (thanks to @delta11 for the proposal)
102113

103114

104115
### New Plugins

build/phaser.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,7 +1289,18 @@ declare module Phaser {
12891289
addText(key: string, url: string, data: Object): void;
12901290
addTextureAtlas(key: string, url: string, data: Object, atlasData: Object, format: number): void;
12911291
addTilemap(key: string, url: string, mapData: Object, format: number): void;
1292+
checkKey(type: number, key: string): boolean;
1293+
checkCanvasKey(key: string): boolean;
12921294
checkImageKey(key: string): boolean;
1295+
checkTextureKey(key: string): boolean;
1296+
checkSoundKey(key: string): boolean;
1297+
checkTextKey(key: string): boolean;
1298+
checkPhysicsKey(key: string): boolean;
1299+
checkTilemapKey(key: string): boolean;
1300+
checkBinaryKey(key: string): boolean;
1301+
checkBitmapDataKey(key: string): boolean;
1302+
checkBitmapFontKey(key: string): boolean;
1303+
checkJSONKey(key: string): boolean;
12931304
decodedSound(key: string, data: Object): void;
12941305
destroy(): void;
12951306
getBinary(key: string): Object;

src/loader/Cache.js

Lines changed: 165 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ Phaser.Cache = function (game) {
9393
*/
9494
this.onSoundUnlock = new Phaser.Signal();
9595

96+
/**
97+
* @property {array} _cacheMap - Const to cache object look-up array.
98+
*/
99+
this._cacheMap = [];
100+
101+
this._cacheMap[Phaser.Cache.CANVAS] = this._canvases;
102+
this._cacheMap[Phaser.Cache.IMAGE] = this._images;
103+
this._cacheMap[Phaser.Cache.TEXTURE] = this._textures;
104+
this._cacheMap[Phaser.Cache.SOUND] = this._sounds;
105+
this._cacheMap[Phaser.Cache.TEXT] = this._text;
106+
this._cacheMap[Phaser.Cache.PHYSICS] = this._physics;
107+
this._cacheMap[Phaser.Cache.TILEMAP] = this._tilemaps;
108+
this._cacheMap[Phaser.Cache.BINARY] = this._binary;
109+
this._cacheMap[Phaser.Cache.BITMAPDATA] = this._bitmapDatas;
110+
this._cacheMap[Phaser.Cache.BITMAPFONT] = this._bitmapFont;
111+
this._cacheMap[Phaser.Cache.JSON] = this._json;
112+
96113
};
97114

98115
/**
@@ -632,15 +649,16 @@ Phaser.Cache.prototype = {
632649
},
633650

634651
/**
635-
* Checks if an image key exists.
652+
* Checks if a key for the given cache object type exists.
636653
*
637-
* @method Phaser.Cache#checkImageKey
654+
* @method Phaser.Cache#checkKey
655+
* @param {number} type - The Cache type to check against. I.e. Phaser.Cache.CANVAS, Phaser.Cache.IMAGE, Phaser.Cache.JSON, etc.
638656
* @param {string} key - Asset key of the image to check is in the Cache.
639657
* @return {boolean} True if the key exists, otherwise false.
640658
*/
641-
checkImageKey: function (key) {
659+
checkKey: function (type, key) {
642660

643-
if (this._images[key])
661+
if (this._cacheMap[type][key])
644662
{
645663
return true;
646664
}
@@ -649,6 +667,149 @@ Phaser.Cache.prototype = {
649667

650668
},
651669

670+
/**
671+
* Checks if the given key exists in the Canvas Cache.
672+
*
673+
* @method Phaser.Cache#checkCanvasKey
674+
* @param {string} key - Asset key of the image to check is in the Cache.
675+
* @return {boolean} True if the key exists, otherwise false.
676+
*/
677+
checkCanvasKey: function (key) {
678+
679+
return this.checkKey(Phaser.Cache.CANVAS, key);
680+
681+
},
682+
683+
/**
684+
* Checks if the given key exists in the Image Cache. Note that this also includes Texture Atlases, Sprite Sheets and Retro Fonts.
685+
*
686+
* @method Phaser.Cache#checkImageKey
687+
* @param {string} key - Asset key of the image to check is in the Cache.
688+
* @return {boolean} True if the key exists, otherwise false.
689+
*/
690+
checkImageKey: function (key) {
691+
692+
return this.checkKey(Phaser.Cache.IMAGE, key);
693+
694+
},
695+
696+
/**
697+
* Checks if the given key exists in the Texture Cache.
698+
*
699+
* @method Phaser.Cache#checkTextureKey
700+
* @param {string} key - Asset key of the image to check is in the Cache.
701+
* @return {boolean} True if the key exists, otherwise false.
702+
*/
703+
checkTextureKey: function (key) {
704+
705+
return this.checkKey(Phaser.Cache.TEXTURE, key);
706+
707+
},
708+
709+
/**
710+
* Checks if the given key exists in the Sound Cache.
711+
*
712+
* @method Phaser.Cache#checkSoundKey
713+
* @param {string} key - Asset key of the image to check is in the Cache.
714+
* @return {boolean} True if the key exists, otherwise false.
715+
*/
716+
checkSoundKey: function (key) {
717+
718+
return this.checkKey(Phaser.Cache.SOUND, key);
719+
720+
},
721+
722+
/**
723+
* Checks if the given key exists in the Text Cache.
724+
*
725+
* @method Phaser.Cache#checkTextKey
726+
* @param {string} key - Asset key of the image to check is in the Cache.
727+
* @return {boolean} True if the key exists, otherwise false.
728+
*/
729+
checkTextKey: function (key) {
730+
731+
return this.checkKey(Phaser.Cache.TEXT, key);
732+
733+
},
734+
735+
/**
736+
* Checks if the given key exists in the Physics Cache.
737+
*
738+
* @method Phaser.Cache#checkPhysicsKey
739+
* @param {string} key - Asset key of the image to check is in the Cache.
740+
* @return {boolean} True if the key exists, otherwise false.
741+
*/
742+
checkPhysicsKey: function (key) {
743+
744+
return this.checkKey(Phaser.Cache.PHYSICS, key);
745+
746+
},
747+
748+
/**
749+
* Checks if the given key exists in the Tilemap Cache.
750+
*
751+
* @method Phaser.Cache#checkTilemapKey
752+
* @param {string} key - Asset key of the image to check is in the Cache.
753+
* @return {boolean} True if the key exists, otherwise false.
754+
*/
755+
checkTilemapKey: function (key) {
756+
757+
return this.checkKey(Phaser.Cache.TILEMAP, key);
758+
759+
},
760+
761+
/**
762+
* Checks if the given key exists in the Binary Cache.
763+
*
764+
* @method Phaser.Cache#checkBinaryKey
765+
* @param {string} key - Asset key of the image to check is in the Cache.
766+
* @return {boolean} True if the key exists, otherwise false.
767+
*/
768+
checkBinaryKey: function (key) {
769+
770+
return this.checkKey(Phaser.Cache.BINARY, key);
771+
772+
},
773+
774+
/**
775+
* Checks if the given key exists in the BitmapData Cache.
776+
*
777+
* @method Phaser.Cache#checkBitmapDataKey
778+
* @param {string} key - Asset key of the image to check is in the Cache.
779+
* @return {boolean} True if the key exists, otherwise false.
780+
*/
781+
checkBitmapDataKey: function (key) {
782+
783+
return this.checkKey(Phaser.Cache.BITMAPDATA, key);
784+
785+
},
786+
787+
/**
788+
* Checks if the given key exists in the BitmapFont Cache.
789+
*
790+
* @method Phaser.Cache#checkBitmapFontKey
791+
* @param {string} key - Asset key of the image to check is in the Cache.
792+
* @return {boolean} True if the key exists, otherwise false.
793+
*/
794+
checkBitmapFontKey: function (key) {
795+
796+
return this.checkKey(Phaser.Cache.BITMAPFONT, key);
797+
798+
},
799+
800+
/**
801+
* Checks if the given key exists in the JSON Cache.
802+
*
803+
* @method Phaser.Cache#checkJSONKey
804+
* @param {string} key - Asset key of the image to check is in the Cache.
805+
* @return {boolean} True if the key exists, otherwise false.
806+
*/
807+
checkJSONKey: function (key) {
808+
809+
return this.checkKey(Phaser.Cache.JSON, key);
810+
811+
},
812+
652813
/**
653814
* Get image data by key.
654815
*

0 commit comments

Comments
 (0)