From 9879fc638728aa486b93f963662881a10e53f7c8 Mon Sep 17 00:00:00 2001 From: photonstorm Date: Wed, 18 Feb 2015 16:58:08 +0000 Subject: [PATCH] If you load an image and provide a key that was already in-use in the Cache, then the old image is now destroyed (via `Cache.removeImage`) and the new image takes its place. --- README.md | 1 + src/loader/Cache.js | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index 97db603be..dac699d89 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ We've also removed functions and properties from Pixi classes that Phaser doesn' * InputHandler.enableDrag with a boundsRect set now takes into account the Sprites anchor when limiting the drag (thanks @unindented #1593) * InputHandler.enableDrag with a boundsSprite set now takes into account both the Sprites anchor and the boundsSprite anchor when limiting the drag. * Sound in Web Audio now uses AudioContext.onended to trigger when it will stop playing instead of using a time based value. This is only used if the sound doesn't loop and isn't an audio sprite, but will give a much more accurate `Sound.onStop` event. It also prevents short audio files from being cut off during playback (#1471) and accounts for time spent decoding. +* If you load an image and provide a key that was already in-use in the Cache, then the old image is now destroyed (via `Cache.removeImage`) and the new image takes its place. ### Bug Fixes diff --git a/src/loader/Cache.js b/src/loader/Cache.js index 54b016946..a3d6964e9 100644 --- a/src/loader/Cache.js +++ b/src/loader/Cache.js @@ -495,6 +495,7 @@ Phaser.Cache.prototype = { /** * 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. * * @method Phaser.Cache#addImage * @param {string} key - The unique key by which you will reference this object. @@ -503,6 +504,11 @@ Phaser.Cache.prototype = { */ addImage: function (key, url, data) { + if (this.checkImageKey(key)) + { + this.removeImage(key); + } + this._images[key] = { url: url, data: data }; this._images[key].frame = new Phaser.Frame(0, 0, 0, data.width, data.height, key, this.game.rnd.uuid());