Options
All
  • Public
  • Public/Protected
  • All
Menu

The Texture object allows games built in Excalibur to load image resources. Texture is an ILoadable which means it can be passed to a Loader to pre-load before starting a level or game.

Textures are the raw image so to add a drawing to a game, you must create a Sprite. You can use Texture.asSprite to quickly generate a Sprite instance.

Pre-loading textures

Pass the Texture to a Loader to pre-load the asset. Once a Texture is loaded, you can generate a Sprite with it.

var txPlayer = new ex.Texture('/assets/tx/player.png');
var loader = new ex.Loader(txPlayer);
game.start(loader).then(function() {
  var player = new ex.Actor();
  player.addDrawing(txPlayer);
  game.add(player);
});

Hierarchy

Implements

Index

Constructors

constructor

  • new Texture(path: string, bustCache?: boolean): Texture

Properties

arrayBuffer

arrayBuffer: ArrayBuffer = null

bustCache

bustCache: boolean

Optionally load texture with cache busting

data

data: HTMLImageElement = null

eventDispatcher

eventDispatcher: EventDispatcher

Direct access to the game object event dispatcher.

height

height: number

The height of the texture in pixels

image

image: HTMLImageElement

Populated once loading is complete

loaded

loaded: Promise<any> = new Promise<any>()

A Promise that resolves when the Texture is loaded.

logger

logger: Logger = Logger.getInstance()

path

path: string

Path to the image resource

responseType

responseType: "" | "arraybuffer" | "blob" | "document" | "json" | "text"

The type to expect as a response: "" | "arraybuffer" | "blob" | "document" | "json" | "text";

width

width: number

The width of the texture in pixels

Methods

asSprite

emit

  • emit(eventName: string, eventObject: GameEvent<any>): void
  • Emits a new event

    Parameters

    • eventName: string

      Name of the event to emit

    • eventObject: GameEvent<any>

      Data associated with this event

    Returns void

getArrayData

  • getArrayData(): any

getData

  • getData(): any

isLoaded

  • isLoaded(): boolean

load

  • load(): Promise<HTMLImageElement>

off

  • off(eventName: string, handler?: function): void
  • Alias for removeEventListener. If only the eventName is specified it will remove all handlers registered for that specific event. If the eventName and the handler instance are specified only that handler will be removed.

    Parameters

    • eventName: string

      Name of the event to listen for

    • Optional handler: function

      Event handler for the thrown event

    Returns void

on

  • on(eventName: string, handler: function): void
  • Alias for addEventListener. You can listen for a variety of events off of the engine; see the events section below for a complete list.

    Parameters

    • eventName: string

      Name of the event to listen for

    • handler: function

      Event handler for the thrown event

    Returns void

once

  • once(eventName: string, handler: function): void
  • Once listens to an event one time, then unsubscribes from that event

    Parameters

    • eventName: string

      The name of the event to subscribe to once

    • handler: function

      The handler of the event that will be auto unsubscribed

    Returns void

oncomplete

  • oncomplete(): void

onerror

  • onerror(): void

onprogress

  • onprogress(): void

processData

  • processData(data: HTMLImageElement): any
  • This method is meant to be overriden to handle any additional processing. Such as decoding downloaded audio bits.

    Parameters

    • data: HTMLImageElement

    Returns any

setData

  • setData(data: any): void

wireEngine

  • wireEngine(_engine: Engine): void

Static extend

  • extend(methods: any): any
  • You may wish to extend native Excalibur functionality in vanilla Javascript. Any method on a class inheriting Class may be extended to support additional functionality. In the example below we create a new type called MyActor.

    var MyActor = Actor.extend({
    
       constructor: function() {
          this.newprop = 'something';
          Actor.apply(this, arguments);
       },
    
       update: function(engine, delta) {
          // Implement custom update
          // Call super constructor update
          Actor.prototype.update.call(this, engine, delta);
    
          console.log("Something cool!");
       }
    });
    
    var myActor = new MyActor(100, 100, 100, 100, Color.Azure);

    In TypeScript, you only need to use the extends syntax, you do not need to use this method of extension.

    Parameters

    • methods: any

      A JSON object contain any methods/properties you want to extend

    Returns any