MainImporter Class

This is the second custom class used in the Bullet Class, MainImporter.

During the same project that prompted TextFormatter it dawned on me I had no real useful Load this class. There are a lot of nice phat loaders around, BulkLoader is a great example. However, it’s kind of overkill for what I wanted.

So I put together MainImporter. It handles Bitmaps, SWFs and XML nicely and I’ve used it extensively for pretty much every project since.

package com.utils{
import com.events.LoaderEvent;
import flash.display.Loader;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class MainImporter extends EventDispatcher{
public static const ITEMLOADED:String = “item loaded”;
private var itemURL:String;
public var loader:Loader;
public var loaderXML:URLLoader;
private var fileEnder:String;
public var loadedCurrent:Number;
public var loadedTotal:Number;
public function MainImporter(_url:String){
itemURL = _url;
var tmpArray:Array = itemURL.split(“.”);
fileEnder = tmpArray[tmpArray.length-1];
loadItem();
}
private function loadItem():void{
var urlRequest: URLRequest = new URLRequest(itemURL);
if(fileEnder == “xml”){
loaderXML = new URLLoader();
loaderXML.addEventListener(IOErrorEvent.IO_ERROR,loadError);
loaderXML.addEventListener(ProgressEvent.PROGRESS,progressListener);
loaderXML.addEventListener(Event.COMPLETE,completeLoad);
loaderXML.addEventListener(Event.INIT,initListener);
loaderXML.load(urlRequest);
}else{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,loadError);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressListener);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeLoad);
loader.contentLoaderInfo.addEventListener(Event.INIT,initListener);
loader.load(urlRequest);
}
}
private function initListener(evt:Event):void{
//trace(“init”);
}
private function progressListener(evt:ProgressEvent):void{
loadedCurrent = Math.floor(evt.bytesLoaded/1024);
loadedTotal = Math.floor(evt.bytesTotal/1024);
dispatchEvent(new LoaderEvent(LoaderEvent.UPDATELOADER,loadedCurrent,loadedTotal,true));
}
private function loadError(evt:IOErrorEvent):void{
trace(“IOErrorEvent : “ + evt);
}
private function completeLoad(evt:Event):void{
dispatchEvent(new Event(MainImporter.ITEMLOADED));
}
}
}

This is a work in progress, so do what you want with it.
MainImporter.as

Leave a Reply

Your email address will not be published. Required fields are marked *