Assetloading in flash
Ever heard of the Asset Factory approach to loading assets in flash? If not here’s a short brief:
1. Loadingorder has always been something to think about in flash, you don’t want to fill your initial load wit to much content, then you can’t actually display even a loading bar to begin with.
2. Opening multiple loading threads can slow down the loading time and increase the tension on the requested server.
3. Keeping track of each object in the loading queue is just a headache if you have enought assets.
Here’s my approach to working with “asset factories”; let’s start with the code:
package nute.net{
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.display.MovieClip;
import flash.net.URLRequest;
public class FactoryLoader{
private var __progress:Number;
private var __assetFactory:Loader;
public var dispatcher:MovieClip;
function FactoryLoader( url:String = null ){
dispatcher = new MovieClip();
if(url!=null){
__load( url )
}
}
public function load( url:String ){
__load( url );
}
function __load( url:String ):void{
__assetFactory = new Loader();
__assetFactory.contentLoaderInfo.addEventListener(Event.COMPLETE, __onComplete);
__assetFactory.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, __onProgress);
__assetFactory.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, __onComplete);
__assetFactory.load( new URLRequest( url) );
}
function __onProgress(evt:ProgressEvent):void{
__progress = evt.bytesLoaded/evt.bytesTotal;
dispatcher.dispatchEvent( new ProgressEvent("progress") );
}
public function get progress():Number{
return __progress;
}
function __onComplete(evt:Event):void{
__assetFactory.contentLoaderInfo.removeEventListener(Event.COMPLETE, __onComplete);
__assetFactory.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, __onProgress);
__assetFactory.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, __onComplete);
if(evt.type == "ioError"){
trace('cant load media')
}else{
__progress = 1;
}
dispatcher.dispatchEvent( evt );
}
public function getObjectByClass( className:String):*{
try{
var ClassObject:Class = __assetFactory.contentLoaderInfo.applicationDomain.getDefinition( className ) as Class;
return new ClassObject();
}catch(e){
return false;
}
}
}
}
This is a way of compiling groups of content in seperate swf’s and loading them as “asset factories”.
What the does this class do then? Well, consider creating a flash file with all the classes you need bundled into it, this FactoryLoader will then load the complete set of classes and make them all available trough the “getObjectByClass” function.
To load assets with a FactoryLoader, you would do something like this:
[...]
var assetLoader:FactoryLoader = new FactoryLoader();
assetLoader.dispatcher.addEventListener("complete", onAssetsLoaded);
assetLoader.dispatcher.addEventListener("progress", onProgress);
assetLoader.load( 'mybakedassets.swf' );
[...]
function onProgress(evt:ProgressEvent):void{
trace( assetLoader.progress );
}
function onAssetsLoaded(evt:Event){
var myNewObject:* = assetLoader.getObjectByClass("MyClassObject");
if(myNewObject){
addChild(myNewObject) /* or whatever you need to do */
}
}