AS3: SequentialLoader
View Example 1
View Example 2
View Documentation
Download Class & Example Files
UPDATE 10/14/08: Added a new parameter that checks policy files for downloading assets from a different server.
The SequentialLoader loads a series of SWFs and/or images in an ordered sequence. This is a conversion of the AS2 SequentialLoader class originally written by Tom Stanley with some additional methods added for more control.
The first example shows the simplest usage of SequentialLoader. The second example shows how to use SequentialLoader with the optional pauseAfterEachLoad property which allows you to run a set of actions after each item has loaded before you resume loading the rest of the sequence. The example loads a small image and then its corresponding large image below it before it goes on to load the next thumb/large image combo.
The class dispatches a custom SequentialLoaderEvent depending on what happens. The events are as follows:
- SequentialLoaderEvent.ON_ITEM_START: Dispatched when an item starts to load (params object contains index)
- SequentialLoaderEvent.ON_ITEM_PROGRESS: Dispatched while an item is loading (params object contains index, percent loaded, bytesLoaded, bytesTotal)
- SequentialLoaderEvent.ON_ITEM_INIT: Dispatched when an item is loaded (params object contains index, asset)
- SequentialLoaderEvent.ON_ITEM_SKIPPED: Dispatched when an item fails to load (params object contains index)
- SequentialLoaderEvent.ON_SEQUENCE_COMPLETE: Dispatched when every item in the sequence has finished loading
Here is the code:
-
/**
-
* The SequentialLoader loads a series of SWFs and/or images in an ordered sequence. This is a conversion of the AS2 SequentialLoader class
-
* originally written by Tom Stanley [http://www.staticmethods.com] with some additional methods added for more control.
-
*
-
* <p>The class dispatches a custom SequentialLoaderEvent depending on what happens. The events are as follows:</p>
-
*
-
* <ul>
-
* <li>SequentialLoaderEvent.ON_ITEM_START: Dispatched when an item starts to load (params object contains index)</li>
-
* <li>SequentialLoaderEvent.ON_ITEM_PROGRESS: Dispatched while an item is loading (params object contains index, percent loaded, bytesLoaded, bytesTotal)</li>
-
* <li>SequentialLoaderEvent.ON_ITEM_INIT: Dispatched when an item is loaded (params object contains index, asset)</li>
-
* <li>SequentialLoaderEvent.ON_ITEM_SKIPPED: Dispatched when an item fails to load (params object contains index)</li>
-
* <li>SequentialLoaderEvent.ON_SEQUENCE_COMPLETE: Dispatched when every item in the sequence has finished loading</li>
-
* </ul>
-
*
-
* @usage
-
* <code>
-
* <pre>
-
import com.reintroducing.loaders.SequentialLoader;
-
import com.reintroducing.events.SequentialLoaderEvent;
-
var assets:Array = new Array("1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg");
-
var sl:SequentialLoader = new SequentialLoader(assets, false);
-
sl.addEventListener(SequentialLoaderEvent.ON_ITEM_START, handleItemStart);
-
sl.addEventListener(SequentialLoaderEvent.ON_ITEM_PROGRESS, handleItemProgress);
-
sl.addEventListener(SequentialLoaderEvent.ON_ITEM_INIT, handleItemInit);
-
sl.addEventListener(SequentialLoaderEvent.ON_ITEM_SKIPPED, handleItemSkipped);
-
sl.addEventListener(SequentialLoaderEvent.ON_SEQUENCE_COMPLETE, handleSequenceComplete);
-
function handleItemStart($evt:SequentialLoaderEvent):void
-
{
-
trace("Item " + $evt.params.index + " has started loading.");
-
}
-
function handleItemProgress($evt:SequentialLoaderEvent):void
-
{
-
trace("Item " + $evt.params.index + " is " + $evt.params.percent + "% loaded");
-
}
-
function handleItemInit($evt:SequentialLoaderEvent):void
-
{
-
var asset:Loader = $evt.params.asset;
-
asset.x = ($evt.params.index * 160);
-
addChild(asset);
-
}
-
function handleItemSkipped($evt:SequentialLoaderEvent):void
-
{
-
trace("Item " + $evt.params.index + " skipped.");
-
}
-
function handleSequenceComplete($evt:SequentialLoaderEvent):void
-
{
-
trace("Sequence completed.");
-
}
-
sl.start();
-
* </pre>
-
* </code>
-
*
-
* @author Matt Przybylski [http://www.reintroducing.com]
-
* @version 1.0
-
*/
-
-
package com.reintroducing.loaders
-
{
-
import flash.display.Loader;
-
import flash.events.Event;
-
import flash.events.EventDispatcher;
-
import flash.events.IOErrorEvent;
-
import flash.events.ProgressEvent;
-
import flash.net.URLRequest;
-
-
import com.reintroducing.events.SequentialLoaderEvent;
-
-
public class SequentialLoader extends EventDispatcher
-
{
-
//- PRIVATE & PROTECTED VARIABLES -------------------------------------------------------------------------
-
-
private var _assets:Array;
-
private var _pauseAfterEachLoad:Boolean;
-
private var _isPaused:Boolean;
-
private var _totalLoads:uint;
-
private var _loader:Loader;
-
private var _currentIndex:uint;
-
private var _currentAsset:String;
-
-
//- PUBLIC & INTERNAL VARIABLES ---------------------------------------------------------------------------
-
-
public static const DEFAULT_NAME:String = "com.reintroducing.loaders.SequentialLoader";
-
-
//- CONSTRUCTOR -------------------------------------------------------------------------------------------
-
-
/**
-
* Creates a new instance of the SequentialLoader class and passes in the assets to load. You can set the optional pauseAfterEachLoad boolean which allows you to
-
* pause after each item is loaded to do another action and then resume the SequentialLoader using the resume() method.
-
*
-
* @usage <pre><code>var sl:SequentialLoader = new SequentialLoader($assets, $pauseAfterEachLoad);</code></pre>
-
*
-
* @param $assets An array that holds the paths to the SWFs/images to be loaded
-
* @param $pauseAfterEachLoad A boolean that allows you to pause the SequentialLoader after each item is loaded
-
*/
-
-
public function SequentialLoader($assets:Array, $pauseAfterEachLoad:Boolean = false):void
-
{
-
this._assets = new Array();
-
-
this._assets = $assets;
-
this._pauseAfterEachLoad = $pauseAfterEachLoad;
-
this._totalLoads = this._assets.length;
-
}
-
-
//- PRIVATE & PROTECTED METHODS ---------------------------------------------------------------------------
-
-
private function loadItem($index:uint):void
-
{
-
this._currentIndex = $index;
-
this._currentAsset = this._assets[this._currentIndex];
-
-
var l:Loader = new Loader();
-
l.contentLoaderInfo.addEventListener(Event.INIT, doLoadInit);
-
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, doProgress);
-
l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, doError);
-
-
l.load(new URLRequest(this._currentAsset));
-
-
this._loader = l;
-
-
var evt:SequentialLoaderEvent = new SequentialLoaderEvent(SequentialLoaderEvent.ON_ITEM_START, {index: this._currentIndex});
-
this.dispatchEvent(evt);
-
}
-
-
private function completeSequence():void
-
{
-
var evt:SequentialLoaderEvent = new SequentialLoaderEvent(SequentialLoaderEvent.ON_SEQUENCE_COMPLETE, {});
-
this.dispatchEvent(evt);
-
}
-
-
private function killEvents():void
-
{
-
this._loader.contentLoaderInfo.removeEventListener(Event.INIT, doLoadInit);
-
this._loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, doProgress);
-
this._loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, doError);
-
this._loader = null;
-
}
-
-
//- PUBLIC & INTERNAL METHODS -----------------------------------------------------------------------------
-
-
/**
-
* Starts the loading of the specified assets.
-
*
-
* @usage <pre><code>sl.start();</code></pre>
-
*
-
* @return Nothing
-
*/
-
-
public function start():void
-
{
-
this.loadItem(0);
-
}
-
-
/**
-
* Pauses the SequentialLoader at its current loading position.
-
*
-
* @usage <pre><code>sl.pause();</code></pre>
-
*
-
* @return Nothing
-
*/
-
-
public function pause():void
-
{
-
this._isPaused = true;
-
}
-
-
/**
-
* Resume the SequentialLoader and load the next asset in the loading sequence.
-
*
-
* @usage <pre><code>sl.resume();</code></pre>
-
*
-
* @return Nothing
-
*/
-
-
public function resume():void
-
{
-
this._isPaused = false;
-
this._currentIndex++;
-
this.loadItem(this._currentIndex);
-
}
-
-
/**
-
* Cleans up the listeners used by the current Loader.
-
*
-
* @usage <pre><code>sl.destroy();</code></pre>
-
*
-
* @return Nothing
-
*/
-
-
public function destroy():void
-
{
-
this.killEvents();
-
}
-
-
//- EVENT HANDLERS ----------------------------------------------------------------------------------------
-
-
private function doProgress($evt:ProgressEvent):void
-
{
-
var percent:Number = Math.ceil(($evt.bytesLoaded / $evt.bytesTotal) * 100);
-
-
var params:Object = new Object();
-
params.index = this._currentIndex;
-
params.percent = percent;
-
params.bytesLoaded = $evt.bytesLoaded;
-
params.bytesTotal = $evt.bytesTotal;
-
-
var evt:SequentialLoaderEvent = new SequentialLoaderEvent(SequentialLoaderEvent.ON_ITEM_PROGRESS, params);
-
this.dispatchEvent(evt);
-
}
-
-
private function doLoadInit($evt:Event):void
-
{
-
var params:Object = new Object();
-
params.index = this._currentIndex;
-
params.asset = $evt.target.loader;
-
-
var evt:SequentialLoaderEvent = new SequentialLoaderEvent(SequentialLoaderEvent.ON_ITEM_INIT, params);
-
this.dispatchEvent(evt);
-
-
this.killEvents();
-
-
if (this._currentIndex <(this._totalLoads - 1))
-
{
-
if (this._isPaused)
-
{
-
return;
-
}
-
else if (this._pauseAfterEachLoad != true)
-
{
-
this._currentIndex++;
-
this.loadItem(this._currentIndex);
-
}
-
}
-
else
-
{
-
this.completeSequence();
-
}
-
}
-
-
private function doError($evt:IOErrorEvent):void
-
{
-
var params:Object = new Object();
-
params.index = this._currentIndex;
-
-
var evt:SequentialLoaderEvent = new SequentialLoaderEvent(SequentialLoaderEvent.ON_ITEM_SKIPPED, params);
-
this.dispatchEvent(evt);
-
-
this.killEvents();
-
-
if (this._currentIndex <this._totalLoads - 1)
-
{
-
if (this._isPaused)
-
{
-
return;
-
}
-
else if (this._pauseAfterEachLoad != true)
-
{
-
this._currentIndex++;
-
this.loadItem(this._currentIndex);
-
}
-
}
-
else
-
{
-
this.completeSequence();
-
}
-
}
-
-
//- GETTERS & SETTERS -------------------------------------------------------------------------------------
-
-
/**
-
* Returns the value of isPaused.
-
*
-
* @usage <pre><code>trace(sl.isPaused);</code></pre>
-
*
-
* @return Boolean
-
*/
-
-
public function get isPaused():Boolean
-
{
-
return this._isPaused;
-
}
-
-
/**
-
* Sets the value of isPaused.
-
*
-
* @usage <pre><code>sl.isPaused = true;</code></pre>
-
*
-
* @return Nothing
-
*/
-
-
public function set isPaused($val:Boolean):void
-
{
-
this._isPaused = $val;
-
}
-
-
/**
-
* Returns the value of pauseAfterEachLoad.
-
*
-
* @usage <pre><code>trace(sl.pauseAfterEachLoad);</code></pre>
-
*
-
* @return Boolean
-
*/
-
-
public function get pauseAfterEachLoad():Boolean
-
{
-
return this._pauseAfterEachLoad;
-
}
-
-
/**
-
* Sets the value of pauseAfterEachLoad.
-
*
-
* @usage <pre><code>sl.pauseAfterEachLoad = true;</code></pre>
-
*
-
* @return Nothing
-
*/
-
-
public function set pauseAfterEachLoad($val:Boolean):void
-
{
-
this._pauseAfterEachLoad = $val;
-
}
-
-
//- HELPERS -----------------------------------------------------------------------------------------------
-
-
public override function toString():String
-
{
-
return "com.reintroducing.loaders.SequentialLoader";
-
}
-
-
//- END CLASS ---------------------------------------------------------------------------------------------
-
}
-
}
If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.
19 Comments
Thanks Lawrence.
Which plugin are you talking about? The code hilighter? If that's the case, its iG:Syntax Hiliter.
[...] made a very small update to the SequentialLoader which allows you to pass in an optional parameter to check for policy files when loading content [...]
This is awesome. Haven't looked into the code yet, but is it possible to make this load images from an XML file?
Joe,
Sure! You just have to parse the paths of the images into an array so that you could pass that array in as the asset array before initializing the SequentialLoader.
Matt,
I got the SequentialLoader to pull images from an XML file.
It works great but I'm also adding movieclip buttons on the stage, one for each image. I want the buttons to correspond to an image. It's simple to associate a button with an image in a standard XML gallery, but not sure how to do that here.
Basically I just want to add one button for each image, then have that image move to the front and alpha tween to 100%.
Joe,
It's hard to say because I don't know how your files are set up, but if you take a look at the Image Slider on this blog you could see how I used the SequentialLoader and then had images corresponding to an xml file as well as being clickable. Hope that helps
Just using your class in project and gotta say - it's a really nice time saver! Big tkank YOU Matt! Best regards!
Hey, great work,i've used it, but, it can be used to download video alongside with swf or images?
cheers
have a nice day
What happens to the params object that is stored in the SequentialLoaderEvent. Does this stay in memory because i don't see anywhere its getting removed. Other than that I think this is a great class and well done in its setup.
@Eric: Unfortunately there is nothing to clear that object, though I'd presume it'd be pretty easy to write something to either clear it or just set it to null when done with it. Either way I don't think the footprint of it is very large at all unless you have hundreds of them. And thanks for the kind words, I've actually built a newer system of managing image loading since this but have yet to release it, probably because you will find out shortly that there is something even better thats going to become available
What if you set your public params to private and used a getter and setter to retrieve it. Would you still need to use a destroy method to clean up the private params property.
"I've actually built a newer system of managing image loading since this but have yet to release it, probably because you will find out shortly that there is something even better thats going to become available"
Please let us know when you release the new version. Do you mean you are releasing something better or someone else is releasing something better?
Thanks,
Eric
@Eric: If you set them to private and use get/set you'll have to create a method to destroy them, yes.
And while I do have something better, someone else has something that is going to "revolutionize" loading of external assets
I've added "params.MCasset = $evt.target.loader.content;" on your SequentialLoader.as file to enable casting into MovieClip.
Btw very good class!! two thumbs up!!
"
private function doLoadInit($evt:Event):void
{
var params:Object = new Object();
params.index = this._currentIndex;
params.asset = $evt.target.loader;
params.MCasset = $evt.target.loader.content;
"













Hello. Just had a brief look and its looking very cool. It's a great contribution to the community. Appreciated.
(PS What plugin is that - can you email it because the one at my blog died..) Thanks