Options
All
  • Public
  • Public/Protected
  • All
Menu

Class AssetLoader

Loads resources for a game. All games have to load resources such as images, sound effects, JSON data, sprite sheets, etc. This class provides a wrapper around the loading of such resources, as well as a callback mechanism to know when loading completes. Games can use this class in a "loading" state, for example.

Currently supported resources include:

  • Images
  • Sound effects
  • JSON data
  • Sprite sheets
  • TMX maps

Hierarchy

  • AssetLoader

Index

Constructors

constructor

  • Provides methods to load images, sounds, and Tiled maps.

    constructor

    Parameters

    • scale: number

      How much to scale image resources.

    • audio: AudioSystem

      A web audio context.

    • Optional assetRoot: string

    Returns AssetLoader

Properties

Private _assetRoot

_assetRoot: any

Private _scale

_scale: any

audio

Private callback

callback: any

Private loadingAssetData

loadingAssetData: any

Private nextCallback

nextCallback: any

Private responses

responses: any

Methods

_completed

  • _completed(res: string, response: any): void
  • Parameters

    • res: string
    • response: any

    Returns void

_isAlreadyTracked

  • _isAlreadyTracked(id: string): boolean
  • Parameters

    • id: string

    Returns boolean

addCanvas

  • addCanvas(id: string, imageSrc: string): void
  • Starts loading a canvas resource.

    Parameters

    • id: string

      The ID to use when retrieving this resource.

    • imageSrc: string

      The URL of the resource.

    Returns void

addImage

  • addImage(id: string, imageSrc: string, firstPixelTranslucent?: boolean): void
  • Starts loading an image resource.

    Parameters

    • id: string

      The ID to use when retrieving this resource.

    • imageSrc: string

      The URL of the resource.

    • Optional firstPixelTranslucent: boolean

      If truthy, the pixel at (0, 0) is made translucent, along with all other pixels of the same color. The default value is false.

    Returns void

addJson

  • addJson(id: string, url?: string): void
  • Starts loading a JSON resource.

    Parameters

    • id: string

      The ID to use when retrieving this resource.

    • Optional url: string

    Returns void

addSound

  • addSound(id: string, soundSrc: string, loopStart?: number, loopByDefaultIfMusic?: boolean): void
  • Starts loading a sound resource.

    Parameters

    • id: string

      The ID to use when retrieving this resource.

    • soundSrc: string

      The URL of the resource.

    • Optional loopStart: number
    • Optional loopByDefaultIfMusic: boolean

    Returns void

addSpriteSheet

  • addSpriteSheet(id: string, imageSrc: string, cellW: number, cellH: number, spacingX?: number, spacingY?: number, firstPixelTranslucent?: boolean): void
  • Starts loading a sprite sheet resource.

    Parameters

    • id: string

      The ID to use when retrieving this resource.

    • imageSrc: string

      The URL of the resource.

    • cellW: number

      The width of a cell.

    • cellH: number

      The height of a cell.

    • Optional spacingX: number

      The horizontal spacing between cells. Assumed to be 0 if not defined.

    • Optional spacingY: number

      The vertical spacing between cells. Assumed to be 0 if not defined.

    • Optional firstPixelTranslucent: boolean

      If truthy, the pixel at (0, 0) is made translucent, along with all other pixels of the same color.

    Returns void

addTmxMap

  • Registers all images needed by the TMX map's tilesets to this asset loader.

    Parameters

    Returns void

get

  • get(res: string): any
  • Retrieves a resource by ID.

    Parameters

    • res: string

      The ID of the resource.

    Returns any

    The resource, or null if not found.

getTmxTilesetImage

  • Returns the image corresponding to a Tiled tileset. This method is called by the library and is typically not called directly by the application.

    Parameters

    Returns Image

    The tileset image.

isDoneLoading

  • isDoneLoading(): boolean
  • Returns whether all assets in thie loader have successfully loaded.

    Returns boolean

    Whether all assets have loaded.

onLoad

  • onLoad(callback: Function): void
  • Parameters

    • callback: Function

    Returns void

set

  • set(res: string, value: any): void
  • Adds a resource.

    Parameters

    • res: string

      The ID for the resource.

    • value: any

      The resource value.

    Returns void

Generated using TypeDoc