Options
All
  • Public
  • Public/Protected
  • All
Menu

The Resource type allows games built in Excalibur to load generic resources. For any type of remote resource it is recommended to use Resource for preloading.

Resource is an ILoadable so it can be passed to a Loader to pre-load before a level or game.

Example usages: JSON, compressed files, blobs.

Pre-loading generic resources

var resLevel1 = new ex.Resource('/assets/levels/1.json', 'application/json');
var loader = new ex.Loader(resLevel1);
// attach a handler to process once loaded
resLevel1.processData = function(data) {
  // process JSON
  var json = JSON.parse(data);
  // create a new level (inherits Scene) with the JSON configuration
  var level = new Level(json);
  // add a new scene
  game.add(level.name, level);
};
game.start(loader);

Type parameters

  • T

Hierarchy

Implements

Index

Constructors

constructor

  • new Resource(path: string, responseType: "" | "arraybuffer" | "blob" | "document" | "json" | "text", bustCache?: boolean): Resource
  • Parameters

    • path: string

      Path to the remote resource

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

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

    • Default value bustCache: boolean = true

      Whether or not to cache-bust requests

    Returns Resource

Properties

arrayBuffer

arrayBuffer: ArrayBuffer = null

bustCache

bustCache: boolean

Whether or not to cache-bust requests

data

data: T = null

eventDispatcher

eventDispatcher: EventDispatcher

Direct access to the game object event dispatcher.

logger

logger: Logger = Logger.getInstance()

path

path: string

Path to the remote resource

responseType

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

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

Methods

emit

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

    Parameters

    • eventName: string

      Name of the event to emit

    • Optional eventObject: GameEvent<any>

      Data associated with this event

    Returns void

getArrayData

  • getArrayData(): any

getData

  • getData(): any

isLoaded

  • isLoaded(): boolean

load

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: T): any
  • This method is meant to be overriden to handle any additional processing. Such as decoding downloaded audio bits.

    Parameters

    • data: T

    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