Loader

Kind of class:class
Inherits from:Dispatcher
Author:Arthur Clemens, Martijn de Visser (rewritten for EventDispatcher usage and added check for already loaded data)
Classpath:org.asapframework.util.loader.Loader
File last modified:Monday, 09 October 2006, 15:26:50
Loads SWF movies or JPEG images into a clip. Multiple/subsequent movies that are added are queued. Loader can deal with multiple loading threads simultaneously (but the real life efficiency is dependent on the capability of the browser).
See also:
Example:
  • Example to load multiple images:
    var loader:Loader = new Loader();
    Listen for feedback during loading:
    loader.addEventListener(LoaderEvent.ON_PROGRESS, this);
    loader.addEventListener(LoaderEvent.ON_DONE, this);
    Load the assets. We will make them invisible first, and turn them visible once they are loaded:
    loader.load( movieholder1_mc, "seaside.jpg", "Seaside", false );
    loader.load( movieholder2_mc, "tree.jpg", "Woods", false );
    To keep track of loading progress, implement the listener method - we will update a progress bar with the incoming data:
    private function onLoadProgress (e:LoaderEvent) : Void {
        progressbar._xscale = 100 / e.total * e.loaded;
    }
    And the method that is called when each asset has finished loading:
    private function onLoadDone (e:LoaderEvent) : Void {
        trace(e.name + " is loaded");
        e.targetClip._visible = true; // or use ActionQueue to fade in the clip
    }
    We could also have implemented LoaderEvent.ON_ALL_LOADED to find out when everything is done.
Events broadcasted to listeners:
  • LoaderEvent with type: ON_ALL_LOADED When no more assets are in the queue
    • (null)
  • LoaderEvent with type: ON_START When a new LoaderWorker starts loading
    • (null)
  • LoaderEvent with type: ON_PROGRESS When some data has been received
    • (null)
  • LoaderEvent with type: ON_DONE When one asset has finished loading
    • (null)
  • LoaderEvent with type: ON_ERROR When an error happened during loading
    • (null)

Summary


Constructor
Instance properties
Instance methods
  • load (inLoc:MovieClip, inUrl:String, inName:String, inIsVisible:Boolean) : Boolean
    • Adds a file to the load queue.
  • getFileCount : Number
  • stopLoading (inName:String) : Void
    • Stops the loading of one file.
  • stopAllLoading : Void
    • Stops the loading of all files, clears the loading queue and frees the loader workers.
  • pauseAllLoading : Void
    • Stops the loading of all files until resumeAllLoading is called.
  • resumeAllLoading : Void
    • Stops the loading of all files until resumeAllLoading is called.
  • isLoading : Boolean
    • The Loader's loading state.
  • getTotalAndLoaded : Object
    • Calculates the total number of bytes loading and loaded of all workers.
  • toString : String
  • getNextInQueue : Void
  • updateLoadingState : Void
    • Updates the loading state; sets mLoadingState to true if one of the workers is loading; to false if all loader workers are free.
  • stopLoadingWorkers (inName:String) : Void
    • Stops all workers loading.
  • removeFromCurrentlyLoading (inFileData:FileData) : Void
    • Removes a FileData object from mCurrentLoadList.
Event handlers

Constructor

Loader

function Loader (
inThreadCount:Number)

Parameters:
inThreadCount:
(optional) The number of threads the Loader should be using. Default (when left empty) is 4. This means that (theoretically) 4 files will be loaded at once. The actual number that Flash uses is dependent on the browser. When the number of threads is 1, the next file will only be loaded when the first is done loading.

Instance properties

mCurrentLoadList

private mCurrentLoadList:Array
(read)

Array of FileData objects that are currently being loaded.

mFileQueue

private mFileQueue:Array
(read)

Array of FileData objects.

mLoadingState

private mLoadingState:Number
(read)

Loading state of the Loader.

mWorkerCount

private mWorkerCount:Number = 4
(read)

The number of LoaderWorker objects that will be doing loading work simultaneously.

mWorkers

private mWorkers:Array
(read)

Array of LoaderWorker objects.

STATE_LOADING

private STATE_LOADING:Number = 1
(read)

STATE_NONE

private STATE_NONE:Number = 0
(read)

STATE_PAUSED

private STATE_PAUSED:Number = 2
(read)

Instance methods

getFileCount

function getFileCount (
) : Number

getNextInQueue

private function getNextInQueue (
) : Void

Events broadcasted to listeners:
  • LoaderEvent with type: ON_ALL_LOADED When no more assets are in the queue
    • (null)
  • LoaderEvent with type: ON_START When a new LoaderWorker starts loading
    • (null)

getTotalAndLoaded

function getTotalAndLoaded (
) : Object

Calculates the total number of bytes loading and loaded of all workers.
Returns:
  • A value object {total:Number, loaded:Number}.

isLoading

function isLoading (
) : Boolean

The Loader's loading state.
Returns:
  • True when one of the workers is still loading, false when all workers have finished.

load

function load (
inLoc:MovieClip, inUrl:String, inName:String, inIsVisible:Boolean) : Boolean

Adds a file to the load queue. The file will be ordered to load as soon as one of the loader workers is idle. Supported file types are: swf, jpg
Parameters:
loc :
Movieclip in where the file should be loaded
url :
File's url or disk location
name :
(optional) Unique identifying name for the loading request
isVisible:
(optional) The visible state of the movieclip once the file is loaded; if not specified, visible = true is assumed
Returns:
  • Error state; false if an error occurred, true if successfully added to the queue

pauseAllLoading

function pauseAllLoading (
) : Void

Stops the loading of all files until resumeAllLoading is called.

removeFromCurrentlyLoading

private function removeFromCurrentlyLoading (
inFileData:FileData) : Void

Removes a FileData object from mCurrentLoadList.
Parameters:
inFileData:
object to remove

resumeAllLoading

function resumeAllLoading (
) : Void

Stops the loading of all files until resumeAllLoading is called.

stopAllLoading

function stopAllLoading (
) : Void

Stops the loading of all files, clears the loading queue and frees the loader workers.

stopLoading

function stopLoading (
inName:String) : Void

Stops the loading of one file.
Parameters:
name:
Name of the loading request as passed with load

stopLoadingWorkers

private function stopLoadingWorkers (
inName:String) : Void

Stops all workers loading.
Parameters:
name:
(optional) name of the loading request as passed with load; if name is passed only the worker with that loading request name will be stopped; otherwise all workers will be stopped

toString

function toString (
) : String

updateLoadingState

private function updateLoadingState (
) : Void

Updates the loading state; sets mLoadingState to true if one of the workers is loading; to false if all loader workers are free.

Event handlers

onWorkerLoadDone

private function onWorkerLoadDone (

Received when a LoaderWorker object is finished or stopped loading. This event is passed on to listeners of the Loader.
Parameters:
e:
event object with properties: .name, .type, .target
Events broadcasted to listeners:
  • LoaderEvent with type: ON_DONE When one asset has finished loading
    • (null)
Implementation note:

onWorkerLoadError

private function onWorkerLoadError (

Received when a LoaderWorker object encounters an error during loading. This event is passed on to listeners of the Loader.
Parameters:
e:
event object with properties: .name, .target
Events broadcasted to listeners:
  • LoaderEvent with type: ON_ERROR When an error happened during loading
    • (null)

onWorkerLoadProgress

private function onWorkerLoadProgress (

Monitors the loading progress of one LoaderWorker object. This event is passed on to listeners of the Loader.
Parameters:
e:
event object with properties: .name, .type, .target
Events broadcasted to listeners:
  • LoaderEvent with type: ON_PROGRESS When some data has been received
    • (null)