From 618891bcda4cd920a3635dd48bf65c8a2204b3fd Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Thu, 15 Sep 2022 22:30:59 +0100 Subject: [PATCH] The `TextureManager.addSpriteSheet` method will now allow you to pass in a Phaser Texture as the 2nd parameter. This allows you to add sprite sheet data to textures that came from external sources, such as SVG files or canvas elements. --- src/textures/TextureManager.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/textures/TextureManager.js b/src/textures/TextureManager.js index 4d6434ff7..c42637813 100644 --- a/src/textures/TextureManager.js +++ b/src/textures/TextureManager.js @@ -848,26 +848,38 @@ var TextureManager = new Class({ * Adds a Sprite Sheet to this Texture Manager. * * In Phaser terminology a Sprite Sheet is a texture containing different frames, but each frame is the exact - * same size and cannot be trimmed or rotated. + * same size and cannot be trimmed or rotated. This is different to a Texture Atlas, created by tools such as + * Texture Packer, and more akin with the fixed-frame exports you get from apps like Aseprite or old arcade + * games. + * + * As of Phaser 3.60 you can use this method to add a sprite sheet to an existing Phaser Texture. * * @method Phaser.Textures.TextureManager#addSpriteSheet * @fires Phaser.Textures.Events#ADD * @since 3.0.0 * - * @param {string} key - The unique string-based key of the Texture. - * @param {HTMLImageElement} source - The source Image element. + * @param {string} key - The unique string-based key of the Texture. Give an empty string if you provide a Phaser Texture as the 2nd argument. + * @param {(HTMLImageElement|Phaser.Textures.Texture)} source - The source Image element, or a Phaser Texture. * @param {Phaser.Types.Textures.SpriteSheetConfig} config - The configuration object for this Sprite Sheet. * - * @return {?Phaser.Textures.Texture} The Texture that was created, or `null` if the key is already in use. + * @return {?Phaser.Textures.Texture} The Texture that was created or updated, or `null` if the key is already in use. */ addSpriteSheet: function (key, source, config) { var texture = null; - if (this.checkKey(key)) + if (source instanceof Texture) + { + key = texture.key; + texture = source; + } + else if (this.checkKey(key)) { texture = this.create(key, source); + } + if (texture) + { var width = texture.source[0].width; var height = texture.source[0].height;