Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Resource<T>

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: string, bustCache?: boolean): Resource
  • Parameters

    • path: string

      Path to the remote resource

    • responseType: string

      The Content-Type to expect (e.g. application/json)

    • Default value bustCache: boolean = true

      Whether or not to cache-bust requests

    Returns Resource

Properties

Private _engine

_engine: Engine

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: string

The Content-Type to expect (e.g. application/json)

Methods

Private _cacheBust

  • _cacheBust(uri: string): string
  • Parameters

    • uri: string

    Returns string

Private _start

  • _start(e: any): void
  • Parameters

    • e: any

    Returns void

emit

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

    Parameters

    • eventName: string

      Name of the event to emit

    • Optional eventObject: GameEvent

      Data associated with this event

    Returns void

getData

  • getData(): any
  • Returns the loaded data once the resource is loaded

    Returns any

isLoaded

  • isLoaded(): boolean
  • Returns true if the Resource is completely loaded and is ready to be drawn.

    Returns boolean

load

  • Begin loading the resource and returns a promise to be resolved on completion

    Returns Promise<T>

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

oncomplete

  • oncomplete(): void
  • Returns void

onerror

  • onerror(): void
  • Returns void

onprogress

  • onprogress(): void
  • Returns 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
  • Sets the data for this resource directly

    Parameters

    • data: any

    Returns 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