var loader : BulkLoader = new BulkLoader("main"); loader.add("bg.jpg"); loader.add("config.xml"); loader.add("soundtrack.mp3"); loader.add("intro.flv"); loader.addEventListener(BulkLoader.PROGRESS, onProgress); loader.addEventListener(BulkLoader.COMPLETE, onComplete); loader.start(); function onProgress(evt : BulkProgressEvent) : void{ trace(evt.percentLoaded); } function onComplete(evt : Event) : void{ var bgBitmap = loader.getBitmap("bg.jpg"); addChild(bgBitmap); var video : Video = new Video(); video.attachNetStream(loader.getNetStream("intro.flv")); parseConfig(loader.getXML("config.xml")); }
//---------------------------------------------------------------------------
// simplest case: loader.add("logo.png"); // use an "id" so the item can be retrieved later without a reference to the url loader.add("background.jpg", {id:"bg"}); // since the url by itself can't tell us what the filetype is, use the type property to let BulkLoader know what to do: loader.add("/some-web-services?size=Large", {type:"image"}); // add an item that should be loaded first (higher priority): loader.add("/data/config.xml", {priority:20}); // add a max try number (defaults to 3) loader.add("/unreliable-web-services.xml", {maxTries:6}); // you can also use a URLRequest object , this will load from a POST request var postRequest : URLRequest = new URLRequest("/save-prefs.php"); postRequest.method = "POST"; var postData : URLVariables = new URLVariables(myPostDataObject ); postRequest.data = postData; loader.add(postRequest, {"id":"settings"}); // of course, all options can be combined: loader.add("the-sound-webservices?name=maintrack", {"id":"soundtrack", type:"sound", maxTries:1, priority:100});
//-------------------------------------------------------------------------------------------
// attaching events to all items: // this will fire once all items have been loaded loader.addEventListener(BulkLoader.COMPLETE, onAllLoaded); // this will fire on progress for any item // the event , BulkProgress is a subclass of ProgressEvent (with extra information) loader.addEventListener(BulkLoader.PROGRESS, onAllProgress); // this will fire if any item fails to load: // the event is BulkErrorEvent and holds an array (errors) with all failed LoadingItem instances loader.addEventListener(BulkLoader.ERROR, onAllError); // you can also listen to events in individual items // this will fire as soon as the item registred with the id of "bg" is done loading (even if there are other items to load) loader.get("bg").addEventListener(Event.COMPLETE,onBackgroundLoaded) // this will only trigged if the config.xml loading fails: loader.get("data/config.xml").addEventListener(BulkLoader.ERROR, onXMLFailed);
//----------------------------------------------------------
var theBgBitmap : Bitmap = loader.getContent("bg") as Bitmap; // you don't need to keep a reference to the loader intance, you can get it by name: var theBgBitmap : Bitmap = BulkLoader.getLoader("main-site").getContent("bg") as Bitmap; // you can also use the conviniece methods to get a typed object: var theBgBitmap : Bitmap = loader.getBitmap("bg"); // grab a BitmapData directly: var theBgBitmap : Bitmap = loader.getBitmapData("bg");