Class: TextureManager

Phaser.Textures. TextureManager

Textures are managed by the global TextureManager. This is a singleton class that is responsible for creating and delivering Textures and their corresponding Frames to Game Objects.

Sprites and other Game Objects get the texture data they need from the TextureManager.

Access it via scene.textures.


new TextureManager(game)

Parameters:
Name Type Description
game Phaser.Game

The Phaser.Game instance this Texture Manager belongs to.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 27)

Extends

Members


game :Phaser.Game

The Game that this TextureManager belongs to.

Type:
Since: 3.0.0
Source: src/textures/TextureManager.js (Line 54)

list :object

An object that has all of textures that Texture Manager creates. Textures are assigned to keys so we can access to any texture that this object has directly by key value without iteration.

Type:
  • object
Since: 3.0.0
Default Value:
  • {}
Source: src/textures/TextureManager.js (Line 72)

name :string

The name of this manager.

Type:
  • string
Since: 3.0.0
Source: src/textures/TextureManager.js (Line 63)

Methods


addAtlas(key, source, data [, dataSource])

Adds a new Texture Atlas to this Texture Manager. It can accept either JSON Array or JSON Hash formats, as exported by Texture Packer and similar software.

Parameters:
Name Type Argument Description
key string

The unique string-based key of the Texture.

source HTMLImageElement

The source Image element.

data object

The Texture Atlas data.

dataSource HTMLImageElement | HTMLCanvasElement | Array.<HTMLImageElement> | Array.<HTMLCanvasElement> <optional>

An optional data Image element.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 539)
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

addAtlasJSONArray(key, source, data [, dataSource])

Adds a Texture Atlas to this Texture Manager. The frame data of the atlas must be stored in an Array within the JSON. This is known as a JSON Array in software such as Texture Packer.

Parameters:
Name Type Argument Description
key string

The unique string-based key of the Texture.

source HTMLImageElement | Array.<HTMLImageElement>

The source Image element/s.

data object | Array.<object>

The Texture Atlas data/s.

dataSource HTMLImageElement | HTMLCanvasElement | Array.<HTMLImageElement> | Array.<HTMLCanvasElement> <optional>

An optional data Image element.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 566)
Fires:
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

addAtlasJSONHash(key, source, data [, dataSource])

Adds a Texture Atlas to this Texture Manager. The frame data of the atlas must be stored in an Object within the JSON. This is known as a JSON Hash in software such as Texture Packer.

Parameters:
Name Type Argument Description
key string

The unique string-based key of the Texture.

source HTMLImageElement

The source Image element.

data object

The Texture Atlas data.

dataSource HTMLImageElement | HTMLCanvasElement | Array.<HTMLImageElement> | Array.<HTMLCanvasElement> <optional>

An optional data Image element.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 619)
Fires:
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

addAtlasXML(key, source, data [, dataSource])

Adds a Texture Atlas to this Texture Manager, where the atlas data is given in the XML format.

Parameters:
Name Type Argument Description
key string

The unique string-based key of the Texture.

source HTMLImageElement

The source Image element.

data object

The Texture Atlas XML data.

dataSource HTMLImageElement | HTMLCanvasElement | Array.<HTMLImageElement> | Array.<HTMLCanvasElement> <optional>

An optional data Image element.

Since: 3.7.0
Source: src/textures/TextureManager.js (Line 666)
Fires:
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

addBase64(key, data)

Adds a new Texture to the Texture Manager created from the given Base64 encoded data.

Parameters:
Name Type Description
key string

The unique string-based key of the Texture.

data *

The Base64 encoded data.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 244)
Fires:
Returns:

This Texture Manager instance.

Type
Phaser.Textures.TextureManager

addCanvas(key, source [, skipCache])

Creates a new Canvas Texture object from an existing Canvas element and adds it to this Texture Manager, unless skipCache is true.

Parameters:
Name Type Argument Default Description
key string

The unique string-based key of the Texture.

source HTMLCanvasElement

The Canvas element to form the base of the new Texture.

skipCache boolean <optional>
false

Skip adding this Texture into the Cache?

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 503)
Fires:
Returns:

The Canvas Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.CanvasTexture

addGLTexture(key, glTexture)

Takes a WebGL Texture and creates a Phaser Texture from it, which is added to the Texture Manager using the given key.

This allows you to then use the Texture as a normal texture for texture based Game Objects like Sprites.

This is a WebGL only feature.

Parameters:
Name Type Description
key string

The unique string-based key of the Texture.

glTexture WebGLTexture

The source Render Texture.

Since: 3.19.0
Source: src/textures/TextureManager.js (Line 381)
Fires:
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

addImage(key, source [, dataSource])

Adds a new Texture to the Texture Manager created from the given Image element.

Parameters:
Name Type Argument Description
key string

The unique string-based key of the Texture.

source HTMLImageElement

The source Image element.

dataSource HTMLImageElement | HTMLCanvasElement <optional>

An optional data Image element.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 347)
Fires:
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

addListener(event, fn [, context])

Add a listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 111)
Returns:

this.

Type
Phaser.Events.EventEmitter

addRenderTexture(key, renderTexture)

Adds a Render Texture to the Texture Manager using the given key. This allows you to then use the Render Texture as a normal texture for texture based Game Objects like Sprites.

Parameters:
Name Type Description
key string

The unique string-based key of the Texture.

renderTexture Phaser.GameObjects.RenderTexture

The source Render Texture.

Since: 3.12.0
Source: src/textures/TextureManager.js (Line 413)
Fires:
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

addSpriteSheet(key, source, config)

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.

Parameters:
Name Type Description
key string

The unique string-based key of the Texture.

source HTMLImageElement

The source Image element.

config Phaser.Types.Textures.SpriteSheetConfig

The configuration object for this Sprite Sheet.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 738)
Fires:
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

addSpriteSheetFromAtlas(key, config)

Adds a Sprite Sheet to this Texture Manager, where the Sprite Sheet exists as a Frame within a Texture Atlas.

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.

Parameters:
Name Type Description
key string

The unique string-based key of the Texture.

config Phaser.Types.Textures.SpriteSheetFromAtlasConfig

The configuration object for this Sprite Sheet.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 773)
Fires:
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

addUnityAtlas(key, source, data [, dataSource])

Adds a Unity Texture Atlas to this Texture Manager. The data must be in the form of a Unity YAML file.

Parameters:
Name Type Argument Description
key string

The unique string-based key of the Texture.

source HTMLImageElement

The source Image element.

data object

The Texture Atlas data.

dataSource HTMLImageElement | HTMLCanvasElement | Array.<HTMLImageElement> | Array.<HTMLCanvasElement> <optional>

An optional data Image element.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 702)
Fires:
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

checkKey(key)

Checks the given texture key and throws a console.warn if the key is already in use, then returns false. If you wish to avoid the console.warn then use TextureManager.exists instead.

Parameters:
Name Type Description
key string

The texture key to check.

Since: 3.7.0
Source: src/textures/TextureManager.js (Line 157)
Returns:

true if it's safe to use the texture key, otherwise false.

Type
boolean

cloneFrame(key, frame)

Takes a Texture key and Frame name and returns a clone of that Frame if found.

Parameters:
Name Type Description
key string

The unique string-based key of the Texture.

frame string | integer

The string or index of the Frame to be cloned.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 902)
Returns:

A Clone of the given Frame.

Type
Phaser.Textures.Frame

create(key, source, width, height)

Creates a new Texture using the given source and dimensions.

Parameters:
Name Type Description
key string

The unique string-based key of the Texture.

source HTMLImageElement

The source Image element.

width integer

The width of the Texture.

height integer

The height of the Texture.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 826)
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

createCanvas(key [, width] [, height])

Creates a new Texture using a blank Canvas element of the size given.

Canvas elements are automatically pooled and calling this method will extract a free canvas from the CanvasPool, or create one if none are available.

Parameters:
Name Type Argument Default Description
key string

The unique string-based key of the Texture.

width integer <optional>
256

The width of the Canvas element.

height integer <optional>
256

The height of the Canvas element.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 473)
Returns:

The Canvas Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.CanvasTexture

destroy()

Destroys the Texture Manager and all Textures stored within it.

Since: 3.0.0
Overrides:
Source: src/textures/TextureManager.js (Line 1142)

each(callback, scope [, args])

Passes all Textures to the given callback.

Parameters:
Name Type Argument Description
callback EachTextureCallback

The callback function to be sent the Textures.

scope object

The value to use as this when executing the callback.

args * <optional>
<repeatable>

Additional arguments that will be passed to the callback, after the child.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 1115)

emit(event [, args])

Calls each of the listeners registered for a given event.

Parameters:
Name Type Argument Description
event string | symbol

The event name.

args * <optional>
<repeatable>

Additional arguments that will be passed to the event handler.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 86)
Returns:

true if the event had listeners, else false.

Type
boolean

eventNames()

Return an array listing the events for which the emitter has registered listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 55)
Returns:
Type
array

exists(key)

Checks the given key to see if a Texture using it exists within this Texture Manager.

Parameters:
Name Type Description
key string

The unique string-based key of the Texture.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 853)
Returns:

Returns true if a Texture matching the given key exists in this Texture Manager.

Type
boolean

generate(key, config)

Creates a new Texture using the given config values. Generated textures consist of a Canvas element to which the texture data is drawn. See the Phaser.Create function for the more direct way to create textures.

Parameters:
Name Type Description
key string

The unique string-based key of the Texture.

config object

The configuration object needed to generate the texture.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 442)
Returns:

The Texture that was created, or null if the key is already in use.

Type
Phaser.Textures.Texture

get(key)

Returns a Texture from the Texture Manager that matches the given key.

If the key is undefined it will return the __DEFAULT Texture.

If the key is an instance of a Texture, it will return the key directly.

Finally. if the key is given, but not found and not a Texture instance, it will return the __MISSING Texture.

Parameters:
Name Type Description
key string | Phaser.Textures.Texture

The unique string-based key of the Texture, or a Texture instance.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 868)
Returns:

The Texture that was created.

Type
Phaser.Textures.Texture

getBase64(key [, frame] [, type] [, encoderOptions])

Gets an existing texture frame and converts it into a base64 encoded image and returns the base64 data.

You can also provide the image type and encoder options.

This will only work with bitmap based texture frames, such as those created from Texture Atlases. It will not work with GL Texture objects, such as Shaders, or Render Textures. For those please see the WebGL Snapshot function instead.

Parameters:
Name Type Argument Default Description
key string

The unique string-based key of the Texture.

frame string | integer <optional>

The string-based name, or integer based index, of the Frame to get from the Texture.

type string <optional>
'image/png'

[description]

encoderOptions number <optional>
0.92

[description]

Since: 3.12.0
Source: src/textures/TextureManager.js (Line 288)
Returns:

The base64 encoded data, or an empty string if the texture frame could not be found.

Type
string

getFrame(key [, frame])

Takes a Texture key and Frame name and returns a reference to that Frame, if found.

Parameters:
Name Type Argument Description
key string

The unique string-based key of the Texture.

frame string | integer <optional>

The string-based name, or integer based index, of the Frame to get from the Texture.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 921)
Returns:

A Texture Frame object.

Type
Phaser.Textures.Frame

getPixel(x, y, key [, frame])

Given a Texture and an x and y coordinate this method will return a new Color object that has been populated with the color and alpha values of the pixel at that location in the Texture.

Parameters:
Name Type Argument Description
x integer

The x coordinate of the pixel within the Texture.

y integer

The y coordinate of the pixel within the Texture.

key string

The unique string-based key of the Texture.

frame string | integer <optional>

The string or index of the Frame.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 964)
Returns:

A Color object populated with the color values of the requested pixel, or null if the coordinates were out of bounds.

Type
Phaser.Display.Color

getPixelAlpha(x, y, key [, frame])

Given a Texture and an x and y coordinate this method will return a value between 0 and 255 corresponding to the alpha value of the pixel at that location in the Texture. If the coordinate is out of bounds it will return null.

Parameters:
Name Type Argument Description
x integer

The x coordinate of the pixel within the Texture.

y integer

The y coordinate of the pixel within the Texture.

key string

The unique string-based key of the Texture.

frame string | integer <optional>

The string or index of the Frame.

Since: 3.10.0
Source: src/textures/TextureManager.js (Line 1011)
Returns:

A value between 0 and 255, or null if the coordinates were out of bounds.

Type
integer

getTextureKeys()

Returns an array with all of the keys of all Textures in this Texture Manager. The output array will exclude the __DEFAULT and __MISSING keys.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 940)
Returns:

An array containing all of the Texture keys stored in this Texture Manager.

Type
Array.<string>

listenerCount(event)

Return the number of listeners listening to a given event.

Parameters:
Name Type Description
event string | symbol

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 75)
Returns:

The number of listeners.

Type
number

listeners(event)

Return the listeners registered for a given event.

Parameters:
Name Type Description
event string | symbol

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 64)
Returns:

The registered listeners.

Type
array

off(event [, fn] [, context] [, once])

Remove the listeners of a given event.

Parameters:
Name Type Argument Description
event string | symbol

The event name.

fn function <optional>

Only remove the listeners that match this function.

context * <optional>

Only remove the listeners that have this context.

once boolean <optional>

Only remove one-time listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 151)
Returns:

this.

Type
Phaser.Events.EventEmitter

on(event, fn [, context])

Add a listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 98)
Returns:

this.

Type
Phaser.Events.EventEmitter

once(event, fn [, context])

Add a one-time listener for a given event.

Parameters:
Name Type Argument Default Description
event string | symbol

The event name.

fn function

The listener function.

context * <optional>
this

The context to invoke the listener with.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 124)
Returns:

this.

Type
Phaser.Events.EventEmitter

remove(key)

Removes a Texture from the Texture Manager and destroys it. This will immediately clear all references to it from the Texture Manager, and if it has one, destroy its WebGLTexture. This will emit a removetexture event.

Note: If you have any Game Objects still using this texture they will start throwing errors the next time they try to render. Make sure that removing the texture is the final step when clearing down to avoid this.

Parameters:
Name Type Description
key string | Phaser.Textures.Texture

The key of the Texture to remove, or a reference to it.

Since: 3.7.0
Source: src/textures/TextureManager.js (Line 181)
Fires:
Returns:

The Texture Manager.

Type
Phaser.Textures.TextureManager

removeAllListeners( [event])

Remove all listeners, or those of the specified event.

Parameters:
Name Type Argument Description
event string | symbol <optional>

The event name.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 165)
Returns:

this.

Type
Phaser.Events.EventEmitter

removeKey(key)

Removes a key from the Texture Manager but does not destroy the Texture that was using the key.

Parameters:
Name Type Description
key string

The key to remove from the texture list.

Since: 3.17.0
Source: src/textures/TextureManager.js (Line 224)
Returns:

The Texture Manager.

Type
Phaser.Textures.TextureManager

removeListener(event [, fn] [, context] [, once])

Remove the listeners of a given event.

Parameters:
Name Type Argument Description
event string | symbol

The event name.

fn function <optional>

Only remove the listeners that match this function.

context * <optional>

Only remove the listeners that have this context.

once boolean <optional>

Only remove one-time listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 137)
Returns:

this.

Type
Phaser.Events.EventEmitter

renameTexture(currentKey, newKey)

Changes the key being used by a Texture to the new key provided.

The old key is removed, allowing it to be re-used.

Game Objects are linked to Textures by a reference to the Texture object, so all existing references will be retained.

Parameters:
Name Type Description
currentKey string

The current string-based key of the Texture you wish to rename.

newKey string

The new unique string-based key to use for the Texture.

Since: 3.12.0
Source: src/textures/TextureManager.js (Line 1081)
Returns:

true if the Texture key was successfully renamed, otherwise false.

Type
boolean

setTexture(gameObject, key [, frame])

Sets the given Game Objects texture and frame properties so that it uses the Texture and Frame specified in the key and frame arguments to this method.

Parameters:
Name Type Argument Description
gameObject Phaser.GameObjects.GameObject

The Game Object the texture would be set on.

key string

The unique string-based key of the Texture.

frame string | integer <optional>

The string or index of the Frame.

Since: 3.0.0
Source: src/textures/TextureManager.js (Line 1057)
Returns:

The Game Object the texture was set on.

Type
Phaser.GameObjects.GameObject

shutdown()

Removes all listeners.

Since: 3.0.0
Inherited From:
Source: src/events/EventEmitter.js (Line 31)