Options
All
  • Public
  • Public/Protected
  • All
Menu

Class AssetManager

The AssetManager handles loading and accessing a variety of asset types. You can add assets directly (via the 'add...' methods) or asynchronously via a queue. This allows you to deal with assets in a unified way, no matter if they are loaded from a file, directory, URL, or from an embedded object.

The class can deal with the following media types:

  • Textures, either from Bitmaps or ATF data
  • Texture atlases
  • Bitmap Fonts
  • Sounds
  • XML data
  • JSON data
  • ByteArrays

For more information on how to add assets from different sources, read the documentation of the "enqueue()" method.

Context Loss

When the stage3D context is lost (and you have enabled 'Starling.handleLostContext'), the AssetManager will automatically restore all loaded textures. To save memory, it will get them from their original sources. Since this is done asynchronously, your images might not reappear all at once, but during a timeframe of several seconds. If you want, you can pause your game during that time; the AssetManager dispatches an "Event.TEXTURES_RESTORED" event when all textures have been restored.

Error handling

Loading of some assets may fail while the queue is being processed. In that case, the AssetManager will dispatch events of type "IO_ERROR", "SECURITY_ERROR" or "PARSE_ERROR". You can listen to those events and handle the errors manually (e.g., you could enqueue them once again and retry, or provide placeholder textures). Queue processing will continue even when those events are dispatched.

Using variable texture formats

When you enqueue a texture, its properties for "format", "scale", "mipMapping", and "repeat" will reflect the settings of the AssetManager at the time they were enqueued. This means that you can enqueue a bunch of textures, then change the settings and enqueue some more. Like this:

appDir:File = File.applicationDirectory; assets:AssetManager = new AssetManager(); assets.textureFormat = Context3DTextureFormat.BGRA; assets.enqueue(appDir.resolvePath("textures/32bit")); assets.textureFormat = Context3DTextureFormat.BGRA_PACKED; assets.enqueue(appDir.resolvePath("textures/16bit")); assets.loadQueue(...);

Hierarchy

Index

Constructors

constructor

  • new AssetManager(scaleFactor?: number, useMipmaps?: boolean): AssetManager

Properties

checkPolicyFile

checkPolicyFile: boolean

Specifies whether a check should be made for the existence of a URL policy file before loading an object from a remote server. More information about this topic can be found in the 'flash.system.LoaderContext' documentation. @default false

forcePotTextures

forcePotTextures: boolean

Indicates if the underlying Stage3D textures should be created as the power-of-two based Texture class instead of the more memory efficient RectangleTexture. @default false

isLoading

isLoading: boolean

Indicates if a queue is currently being loaded.

keepAtlasXmls

keepAtlasXmls: boolean

Indicates if atlas XML data should be stored for access via the 'getXml' method. If true, you can access an XML under the same name as the atlas. If false, XMLs will be disposed when the atlas was created. @default false.

keepFontXmls

keepFontXmls: boolean

Indicates if bitmap font XML data should be stored for access via the 'getXml' method. If true, you can access an XML under the same name as the bitmap font. If false, XMLs will be disposed when the font was created. @default false.

numConnections

numConnections: number

The maximum number of parallel connections that are spawned when loading the queue. More connections can reduce loading times, but require more memory. @default 3.

numQueuedAssets

numQueuedAssets: number

Returns the number of raw assets that have been enqueued, but not yet loaded.

Protected queue

queue: Array<any>

The queue contains one 'Object' for each enqueued asset. Each object has 'asset' and 'name' properties, pointing to the raw asset and its name, respectively.

registerBitmapFontsWithFontFace

registerBitmapFontsWithFontFace: boolean

Indicates if bitmap fonts should be registered with their "face" attribute from the font XML file. Per default, they are registered with the name of the texture file. @default false

scaleFactor

scaleFactor: number

Textures that are created from Bitmaps or ATF files will have the scale factor assigned here. @default 1

textureFormat

textureFormat: Context3DTextureFormat

Textures that are created from Bitmaps will be uploaded to the GPU with the Context3DTextureFormat assigned to this property. @default "bgra"

useMipMaps

useMipMaps: boolean

For bitmap textures, this flag indicates if mip maps should be generated when they are loaded; for ATF textures, it indicates if mip maps are valid and should be used. @default false

verbose

verbose: boolean

When activated, the class will trace information about added/enqueued assets.

default

true

Methods

addBitmapFont

  • addBitmapFont(name: string, font: BitmapFont): void
  • Register a bitmap font under a certain name. It will be available right away. If the name was already taken, the existing font will be disposed and replaced by the new one.

    Note that the font is not registered at the TextField class. This only happens when a bitmap font is loaded via the asset queue.

    Parameters

    Returns void

addByteArray

  • addByteArray(name: string, byteArray: ByteArray): void
  • Register a byte array under a certain name. It will be available right away. If the name was already taken, the existing byte array will be cleared and replaced by the new one.

    Parameters

    • name: string
    • byteArray: ByteArray

    Returns void

addEventListener

  • addEventListener(type: string, listener: Function): void

addObject

  • addObject(name: string, object: any): void
  • Register an arbitrary object under a certain name. It will be available right away. If the name was already taken, the existing object will be replaced by the new one.

    Parameters

    • name: string
    • object: any

    Returns void

addSound

  • addSound(name: string, sound: Sound): void
  • Register a sound under a certain name. It will be available right away. If the name was already taken, the existing sound will be replaced by the new one.

    Parameters

    • name: string
    • sound: Sound

    Returns void

addTexture

  • addTexture(name: string, texture: Texture): void
  • Register a texture under a certain name. It will be available right away. If the name was already taken, the existing texture will be disposed and replaced by the new one.

    Parameters

    Returns void

addTextureAtlas

  • Register a texture atlas under a certain name. It will be available right away. If the name was already taken, the existing atlas will be disposed and replaced by the new one.

    Parameters

    Returns void

addXml

  • addXml(name: string, xml: any): void
  • Register an XML object under a certain name. It will be available right away. If the name was already taken, the existing XML will be disposed and replaced by the new one.

    Parameters

    • name: string
    • xml: any

    Returns void

dispatchEvent

  • dispatchEvent(event: Event): void
  • Dispatches an event to all objects that have registered listeners for its type. If an event with enabled 'bubble' property is dispatched to a display object, it will travel up along the line of parents, until it either hits the root object or someone stops its propagation manually.

    Parameters

    Returns void

dispatchEventWith

  • dispatchEventWith(type: string, bubbles?: boolean, data?: any): void

dispose

  • dispose(): void
  • Disposes all contained textures, XMLs and ByteArrays.

    Beware that all references to the assets will remain intact, even though the assets are no longer valid. Call 'purge' if you want to remove all resources and reuse the AssetManager later.

    Returns void

enqueue

  • enqueue(rawAssets: Array<any>): void
  • Enqueues one or more raw assets; they will only be available after successfully executing the "loadQueue" method. This method accepts a variety of different objects:

    • Strings or URLRequests containing an URL to a local or remote resource. Supported types: png, jpg, gif, atf, mp3, xml, fnt, json, binary.
    • Instances of the File class (AIR only) pointing to a directory or a file. Directories will be scanned recursively for all supported types.
    • Classes that contain static embedded assets.
    • If the file extension is not recognized, the data is analyzed to see if contains XML or JSON data. If it's neither, it is stored as ByteArray.

    Suitable object names are extracted automatically: A file named "image.png" will be accessible under the name "image". When enqueuing embedded assets via a class, the variable name of the embedded object will be used as its name. An exception are texture atlases: they will have the same name as the actual texture they are referencing.

    XMLs that contain texture atlases or bitmap fonts are processed directly: fonts are registered at the TextField class, atlas textures can be acquired with the "getTexture()" method. All other XMLs are available via "getXml()".

    If you pass in JSON data, it will be parsed into an object and will be available via "getObject()".

    Parameters

    • rawAssets: Array<any>

    Returns void

enqueueWithName

  • enqueueWithName(asset: any, name?: string, options?: TextureOptions): string
  • Enqueues a single asset with a custom name that can be used to access it later. If the asset is a texture, you can also add custom texture options.

    Parameters

    • asset: any

      The asset that will be enqueued; accepts the same objects as the 'enqueue' method.

    • Optional name: string

      The name under which the asset will be found later. If you pass null or omit the parameter, it's attempted to generate a name automatically.

    • Optional options: TextureOptions

      Custom options that will be used if 'asset' points to texture data.

    Returns string

    the name with which the asset was registered.

getBitmapFont

getBitmapFontNames

  • getBitmapFontNames(prefix?: string, out?: Vector<string>): Vector<string>
  • Returns all bitmap fonts names that start with a certain string, sorted alphabetically. If you pass an out-vector, the names will be added to that vector.

    Parameters

    • Optional prefix: string
    • Optional out: Vector<string>

    Returns Vector<string>

getByteArray

  • getByteArray(name: string): ByteArray

getByteArrayNames

  • getByteArrayNames(prefix?: string, out?: Vector<string>): Vector<string>
  • Returns all byte array names that start with a certain string, sorted alphabetically. If you pass an out-vector, the names will be added to that vector.

    Parameters

    • Optional prefix: string
    • Optional out: Vector<string>

    Returns Vector<string>

getObject

  • getObject(name: string): any
  • Returns an object with a certain name, or null if it's not found. Enqueued JSON data is parsed and can be accessed with this method.

    Parameters

    • name: string

    Returns any

getObjectNames

  • getObjectNames(prefix?: string, out?: Vector<string>): Vector<string>
  • Returns all object names that start with a certain string, sorted alphabetically. If you pass an out-vector, the names will be added to that vector.

    Parameters

    • Optional prefix: string
    • Optional out: Vector<string>

    Returns Vector<string>

getSound

  • getSound(name: string): Sound

getSoundNames

  • getSoundNames(prefix?: string, out?: Vector<string>): Vector<string>
  • Returns all sound names that start with a certain string, sorted alphabetically. If you pass an out-vector, the names will be added to that vector.

    Parameters

    • Optional prefix: string
    • Optional out: Vector<string>

    Returns Vector<string>

getTexture

  • getTexture(name: string): Texture
  • Returns a texture with a certain name. The method first looks through the directly added textures; if no texture with that name is found, it scans through all texture atlases.

    Parameters

    • name: string

    Returns Texture

getTextureAtlas

getTextureAtlasNames

  • getTextureAtlasNames(prefix?: string, out?: Vector<string>): Vector<string>
  • Returns all texture atlas names that start with a certain string, sorted alphabetically. If you pass an out-vector, the names will be added to that vector.

    Parameters

    • Optional prefix: string
    • Optional out: Vector<string>

    Returns Vector<string>

getTextureNames

  • getTextureNames(prefix?: string, out?: Vector<string>): Vector<string>
  • Returns all texture names that start with a certain string, sorted alphabetically.

    Parameters

    • Optional prefix: string
    • Optional out: Vector<string>

    Returns Vector<string>

getTextures

  • getTextures(prefix?: string, out?: Vector<Texture>): Vector<Texture>

getXml

  • getXml(name: string): any

getXmlNames

  • getXmlNames(prefix?: string, out?: Vector<string>): Vector<string>
  • Returns all XML names that start with a certain string, sorted alphabetically. If you pass an out-vector, the names will be added to that vector.

    Parameters

    • Optional prefix: string
    • Optional out: Vector<string>

    Returns Vector<string>

Protected get_checkPolicyFile

  • get_checkPolicyFile(): boolean

Protected get_forcePotTextures

  • get_forcePotTextures(): boolean

Protected get_isLoading

  • get_isLoading(): boolean

Protected get_keepAtlasXmls

  • get_keepAtlasXmls(): boolean

Protected get_keepFontXmls

  • get_keepFontXmls(): boolean

Protected get_numConnections

  • get_numConnections(): number

Protected get_numQueuedAssets

  • get_numQueuedAssets(): number

Protected get_queue

  • get_queue(): Array<any>

get_registerBitmapFontsWithFontFace

  • get_registerBitmapFontsWithFontFace(): boolean

Protected get_scaleFactor

  • get_scaleFactor(): number

Protected get_textureFormat

  • get_textureFormat(): Context3DTextureFormat

Protected get_useMipMaps

  • get_useMipMaps(): boolean

Protected get_verbose

  • get_verbose(): boolean

hasEventListener

  • hasEventListener(type: string, listener?: any): boolean

loadQueue

  • loadQueue(onProgress: function): void
  • Loads all enqueued assets asynchronously. The 'onProgress' will be called with a 'ratio' between '0.0' and '1.0', with '1.0' meaning that it's complete.

    When you call this method, the manager will save a reference to "Starling.current"; all textures that are loaded will be accessible only from within this instance. Thus, if you are working with more than one Starling instance, be sure to call "makeCurrent()" on the appropriate instance before processing the queue.

    Parameters

    • onProgress: function

      function(ratio:Number):void;

        • (number: any): void
        • Parameters

          • number: any

          Returns void

    Returns void

playSound

  • playSound(name: string, startTime?: number, loops?: number, transform?: SoundTransform): SoundChannel
  • Generates a new SoundChannel object to play back the sound. This method returns a SoundChannel object, which you can access to stop the sound and to control volume.

    Parameters

    • name: string
    • Optional startTime: number
    • Optional loops: number
    • Optional transform: SoundTransform

    Returns SoundChannel

purge

  • purge(): void

purgeQueue

  • purgeQueue(): void

removeBitmapFont

  • removeBitmapFont(name: string, dispose?: boolean): void

removeByteArray

  • removeByteArray(name: string, dispose?: boolean): void

removeEventListener

  • removeEventListener(type: string, listener: Function): void

removeEventListeners

  • removeEventListeners(type?: string): void

removeObject

  • removeObject(name: string): void

removeSound

  • removeSound(name: string): void

removeTexture

  • removeTexture(name: string, dispose?: boolean): void

removeTextureAtlas

  • removeTextureAtlas(name: string, dispose?: boolean): void

removeXml

  • removeXml(name: string, dispose?: boolean): void

Protected set_checkPolicyFile

  • set_checkPolicyFile(value: boolean): boolean

Protected set_forcePotTextures

  • set_forcePotTextures(value: boolean): boolean

Protected set_keepAtlasXmls

  • set_keepAtlasXmls(value: boolean): boolean

Protected set_keepFontXmls

  • set_keepFontXmls(value: boolean): boolean

Protected set_numConnections

  • set_numConnections(value: number): number

set_registerBitmapFontsWithFontFace

  • set_registerBitmapFontsWithFontFace(value: boolean): boolean

Protected set_scaleFactor

  • set_scaleFactor(value: number): number

Protected set_textureFormat

  • set_textureFormat(value: Context3DTextureFormat): Context3DTextureFormat

Protected set_useMipMaps

  • set_useMipMaps(value: boolean): boolean

Protected set_verbose

  • set_verbose(value: boolean): boolean

Generated using TypeDoc