phaser - v4.0.0-rc.4
    Preparing search index...

    When Phaser boots it will create an instance of this Texture Manager class.

    It is a global manager that handles all textures in your game. You can access it from within a Scene via the this.textures property.

    Its role is as a manager for all textures that your game uses. It can create, update and remove textures globally, as well as parse texture data from external files, such as sprite sheets and texture atlases.

    Sprites and other texture-based Game Objects get their texture data directly from this class.

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    game: Game

    The Game that the Texture Manager belongs to.

    A game will only ever have one instance of a Texture Manager.

    list: object

    This object contains all Textures that belong to this Texture Manager.

    Textures are identified by string-based keys, which are used as the property within this object. Therefore, you can access any texture directly from this object without any iteration.

    You should not typically modify this object directly, but instead use the methods provided by the Texture Manager to add and remove entries from it.

    name: string

    The internal name of this manager.

    silentWarnings: boolean

    If this flag is true then the Texture Manager will never emit any warnings to the console log that report missing textures.

    stamp: Stamp

    An Image Game Object that belongs to this Texture Manager.

    Used as a drawing stamp within Dynamic Textures.

    This is not part of the display list and doesn't render.

    Prior to v4.0.0, this was of the type Phaser.GameObjects.Image.

    stampCrop: Geom.Rectangle

    The crop Rectangle as used by the Stamp when it needs to crop itself.

    A TileSprite Game Object that belongs to this Texture Manager.

    Used for repeated drawing within Dynamic Textures.

    This is not part of the display list and doesn't render.

    Methods

    • Adds a Texture Atlas to this Texture Manager.

      In Phaser terminology, a Texture Atlas is a combination of an atlas image and a JSON data file, such as those exported by applications like Texture Packer.

      It can accept either JSON Array or JSON Hash formats, as exported by Texture Packer and similar software.

      As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • source: Textures.Texture | HTMLImageElement | HTMLImageElement[]

        The source Image element/s, or a Phaser Texture.

      • data: object | object[]

        The Texture Atlas data/s.

      • OptionaldataSource: HTMLImageElement | HTMLCanvasElement | HTMLImageElement[] | HTMLCanvasElement[]

        An optional data Image element.

      Returns Textures.Texture

    • Adds a Texture Atlas to this Texture Manager.

      In Phaser terminology, a Texture Atlas is a combination of an atlas image and a JSON data file, such as those exported by applications like Texture Packer.

      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.

      As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • source: Textures.Texture | HTMLImageElement | HTMLImageElement[]

        The source Image element/s, or a Phaser Texture.

      • data: object | object[]

        The Texture Atlas data/s.

      • OptionaldataSource: HTMLImageElement | HTMLCanvasElement | HTMLImageElement[] | HTMLCanvasElement[]

        An optional data Image element.

      Returns Textures.Texture

    • Adds a Texture Atlas to this Texture Manager.

      In Phaser terminology, a Texture Atlas is a combination of an atlas image and a JSON data file, such as those exported by applications like Texture Packer.

      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.

      As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • source: Textures.Texture | HTMLImageElement | HTMLImageElement[]

        The source Image element/s, or a Phaser Texture.

      • data: object | object[]

        The Texture Atlas data/s.

      • OptionaldataSource: HTMLImageElement | HTMLCanvasElement | HTMLImageElement[] | HTMLCanvasElement[]

        An optional data Image element.

      Returns Textures.Texture

    • Adds a Texture Atlas to this Texture Manager.

      In Phaser terminology, a Texture Atlas is a combination of an atlas image and a data file, such as those exported by applications like Texture Packer.

      The frame data of the atlas must be stored in an XML file.

      As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • source: Textures.Texture | HTMLImageElement

        The source Image element, or a Phaser Texture.

      • data: object

        The Texture Atlas XML data.

      • OptionaldataSource: HTMLImageElement | HTMLCanvasElement | HTMLImageElement[] | HTMLCanvasElement[]

        An optional data Image element.

      Returns Textures.Texture

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

      It works by creating an Image DOM object, then setting the src attribute to the given base64 encoded data. As a result, the process is asynchronous by its nature, so be sure to listen for the events this method dispatches before using the texture.

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • data: any

        The Base64 encoded data.

      Returns this

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

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • source: HTMLCanvasElement

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

      • OptionalskipCache: boolean

        Skip adding this Texture into the Cache? Default false.

      Returns Textures.CanvasTexture

    • Adds a Compressed Texture to this Texture Manager.

      The texture should typically have been loaded via the CompressedTextureFile loader, in order to prepare the correct data object this method requires.

      You can optionally also pass atlas data to this method, in which case a texture atlas will be generated from the given compressed texture, combined with the atlas data.

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • textureData: CompressedTextureData

        The Compressed Texture data object.

      • OptionalatlasData: object

        Optional Texture Atlas data.

      Returns Textures.Texture

    • Creates a Dynamic Texture instance and adds itself to this Texture Manager.

      A Dynamic Texture is a special texture that allows you to draw textures, frames and most kind of Game Objects directly to it.

      You can take many complex objects and draw them to this one texture, which can then be used as the base texture for other Game Objects, such as Sprites. Should you then update this texture, all Game Objects using it will instantly be updated as well, reflecting the changes immediately.

      It's a powerful way to generate dynamic textures at run-time that are WebGL friendly and don't invoke expensive GPU uploads on each change.

      See the methods available on the DynamicTexture class for more details.

      Optionally, you can also pass a Dynamic Texture instance to this method to have it added to the Texture Manager.

      Parameters

      • key: string | DynamicTexture

        The string-based key of this Texture. Must be unique within the Texture Manager. Or, a DynamicTexture instance.

      • Optionalwidth: number

        The width of this Dynamic Texture in pixels. Defaults to 256 x 256. Ignored if an instance is passed as the key. Default 256.

      • Optionalheight: number

        The height of this Dynamic Texture in pixels. Defaults to 256 x 256. Ignored if an instance is passed as the key. Default 256.

      • OptionalforceEven: boolean

        If the width and height are odd numbers, should they be forced to be even? Ignored if an instance is passed as the key. Default true.

      Returns DynamicTexture

    • Takes a WebGLTextureWrapper 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.

      Prior to Phaser 3.80.0, this method took a bare WebGLTexture as the glTexture parameter. You must now wrap the WebGLTexture in a WebGLTextureWrapper instance before passing it to this method.

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • glTexture: WebGLTextureWrapper

        The source Render Texture.

      Returns Textures.Texture

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

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • source: HTMLImageElement

        The source Image element.

      • OptionaldataSource: HTMLImageElement | HTMLCanvasElement

        An optional data Image element.

      Returns Textures.Texture

    • Add a listener for a given event.

      Parameters

      • event: string | symbol

        The event name.

      • fn: Function

        The listener function.

      • Optionalcontext: any

        The context to invoke the listener with. Default this.

      Returns this

    • 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. 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.

      Parameters

      • key: string

        The unique string-based key of the Texture. Give an empty string if you provide a Phaser Texture as the 2nd argument.

      • source: Textures.Texture | HTMLImageElement

        The source Image element, or a Phaser Texture.

      • config: SpriteSheetConfig

        The configuration object for this Sprite Sheet.

      • OptionaldataSource: HTMLImageElement | HTMLCanvasElement

        An optional data Image element.

      Returns Textures.Texture

    • 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

      • key: string

        The unique string-based key of the Texture.

      • config: SpriteSheetFromAtlasConfig

        The configuration object for this Sprite Sheet.

      Returns Textures.Texture

    • Creates a texture from an array of colour data.

      This is only available in WebGL mode.

      If the dimensions provided are powers of two, the resulting texture will be automatically set to wrap by the WebGL Renderer.

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • data: Uint8Array

        The color data for the texture.

      • width: number

        The width of the texture.

      • height: number

        The height of the texture.

      Returns Textures.Texture

    • Adds a Unity Texture Atlas to this Texture Manager.

      In Phaser terminology, a Texture Atlas is a combination of an atlas image and a data file, such as those exported by applications like Texture Packer or Unity.

      The frame data of the atlas must be stored in a Unity YAML file.

      As of Phaser 3.60 you can use this method to add a atlas data to an existing Phaser Texture.

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • source: HTMLImageElement

        The source Image element.

      • data: object

        The Texture Atlas data.

      • OptionaldataSource: HTMLImageElement | HTMLCanvasElement | HTMLImageElement[] | HTMLCanvasElement[]

        An optional data Image element.

      Returns Textures.Texture

    • 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

      • key: string

        The texture key to check.

      Returns boolean

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

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • frame: string | number

        The string or index of the Frame to be cloned.

      Returns Frame

    • Creates a new Texture using the given source and dimensions.

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • source:
            | WebGLTextureWrapper
            | HTMLImageElement
            | HTMLCanvasElement
            | HTMLImageElement[]
            | HTMLCanvasElement[]

        An array of sources that are used to create the texture. Usually Images, but can also be a Canvas.

      • Optionalwidth: number

        The width of the Texture. This is optional and automatically derived from the source images.

      • Optionalheight: number

        The height of the Texture. This is optional and automatically derived from the source images.

      Returns Textures.Texture

    • 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

      • key: string

        The unique string-based key of the Texture.

      • Optionalwidth: number

        The width of the Canvas element. Default 256.

      • Optionalheight: number

        The height of the Canvas element. Default 256.

      Returns Textures.CanvasTexture

    • Passes all Textures to the given callback.

      Parameters

      • callback: EachTextureCallback

        The callback function to be sent the Textures.

      • scope: object

        The value to use as this when executing the callback.

      • ...args: any[]

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

      Returns void

    • Calls each of the listeners registered for a given event.

      Parameters

      • event: string | symbol

        The event name.

      • ...args: any[]

        Additional arguments that will be passed to the event handler.

      Returns boolean

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

      Parameters

      • key: string

        The unique string-based key of the Texture.

      Returns boolean

    • 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 instance.

      If the key is an instance of a Frame, it will return the frames parent Texture instance.

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

      Parameters

      • key: string | Textures.Texture | Frame

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

      Returns Textures.Texture

    • 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

      • key: string

        The unique string-based key of the Texture.

      • Optionalframe: string | number

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

      • Optionaltype: string

        A DOMString indicating the image format. The default format type is image/png. Default 'image/png'.

      • OptionalencoderOptions: number

        A Number between 0 and 1 indicating the image quality to use for image formats that use lossy compression such as image/jpeg and image/webp. If this argument is anything else, the default value for image quality is used. The default value is 0.92. Other arguments are ignored. Default 0.92.

      Returns string

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

      Parameters

      • key: string

        The unique string-based key of the Texture.

      • Optionalframe: string | number

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

      Returns 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

      • x: number

        The x coordinate of the pixel within the Texture.

      • y: number

        The y coordinate of the pixel within the Texture.

      • key: string

        The unique string-based key of the Texture.

      • Optionalframe: string | number

        The string or index of the Frame.

      Returns Display.Color

    • 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

      • x: number

        The x coordinate of the pixel within the Texture.

      • y: number

        The y coordinate of the pixel within the Texture.

      • key: string

        The unique string-based key of the Texture.

      • Optionalframe: string | number

        The string or index of the Frame.

      Returns number

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

      Returns string[]

    • Return the number of listeners listening to a given event.

      Parameters

      • event: string | symbol

        The event name.

      Returns number

    • Return the listeners registered for a given event.

      Parameters

      • event: string | symbol

        The event name.

      Returns Function[]

    • Remove the listeners of a given event.

      Parameters

      • event: string | symbol

        The event name.

      • Optionalfn: Function

        Only remove the listeners that match this function.

      • Optionalcontext: any

        Only remove the listeners that have this context.

      • Optionalonce: boolean

        Only remove one-time listeners.

      Returns this

    • Add a listener for a given event.

      Parameters

      • event: string | symbol

        The event name.

      • fn: Function

        The listener function.

      • Optionalcontext: any

        The context to invoke the listener with. Default this.

      Returns this

    • Add a one-time listener for a given event.

      Parameters

      • event: string | symbol

        The event name.

      • fn: Function

        The listener function.

      • Optionalcontext: any

        The context to invoke the listener with. Default this.

      Returns this

    • Parses the 'key' parameter and returns a Texture Frame instance.

      It can accept the following formats:

      1. A string
      2. An array where the elements are: [ key, [frame] ]
      3. An object with the properties: { key, [frame] }
      4. A Texture instance - which returns the default frame from the Texture
      5. A Frame instance - returns itself

      Parameters

      Returns Frame

    • 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

      • key: string | Textures.Texture

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

      Returns TextureManager

    • Remove the listeners of a given event.

      Parameters

      • event: string | symbol

        The event name.

      • Optionalfn: Function

        Only remove the listeners that match this function.

      • Optionalcontext: any

        Only remove the listeners that have this context.

      • Optionalonce: boolean

        Only remove one-time listeners.

      Returns this

    • 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

      • 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.

      Returns boolean

    • Resets the internal Stamp object, ready for drawing and returns it.

      Parameters

      • Optionalalpha: number

        The alpha to use. Default 1.

      • Optionaltint: number

        WebGL only. The tint color to use. Default 0xffffff.

      Returns GameObjects.Image

    • Resets the internal Tile Sprite object, ready for drawing, and returns it.

      Parameters

      • Optionalalpha: number

        The alpha to use. Default 1.

      • Optionaltint: number

        WebGL only. The tint color to use. Default 0xffffff.

      Returns GameObjects.TileSprite

    • 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

      • gameObject: GameObject

        The Game Object the texture would be set on.

      • key: string

        The unique string-based key of the Texture.

      • Optionalframe: string | number

        The string or index of the Frame.

      Returns GameObject