/ Published in: ActionScript 3
This is VERY BASIC loading class, the idea is to load array of images and when they're loaded you can retrve them via ChromeLoader.getContent() method, just pass ID. In the constructor you got prepend url (for example images/) and "backup" (example "/backup" ). This will be used if the loader... The names of the functions are self-exaplanary, this is perfect solution for lightwave banners.
v 1.1 - GarbageCollection added
v 1.1 - GarbageCollection added
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* Author : Chrysto Dimchev ( burnandbass [at] gmail [.] com ) */ package { import flash.display.MovieClip; import flash.display.DisplayObject; import flash.display.Loader; import flash.display.LoaderInfo; import flash.events.EventDispatcher; import flash.events.Event; import flash.events.ProgressEvent; import flash.events.IOErrorEvent; import flash.net.URLRequest; import flash.utils.Dictionary; import flash.system.System; public class ChromeLoader extends EventDispatcher { private static var _content:Array = []; private static var _contentNames:Dictionary = new Dictionary(true); private var _loadersCompleted:Number = 0; private var _loaders:Array = []; private var _loaderNames:Array = []; private var _maxTries:Number = 0; private var _site:String = ""; private var _backupUrl:String = ""; private var _finished:Boolean = false; /* CONSTRCTOR pass params: @site --> url to prepend the loaders (expl: "http://site.com/images") @backupURL --> backup url, the loader will try to load the same picture, but with this string instead ot @site (expl: http://sitebackup.com/images) */ public function ChromeLoader(site:String = null, backupURL:String = null):void { if(site){ _site = site } if(backupURL){ _backupUrl = backupURL } } //// PUBLIC //load images/MovieClips, pass array of items to load public function loadContents(_items:Array):void{ for(var i:Number = 0; i< _items.length; i++){ _loaderNames.push( _items[i] ); } loadContent(_loaderNames[_loadersCompleted]); } //returns loader for given ID public function getLoader(_contentID:Number):Loader{ if(_loaders[_contentID]){ return _loaders[_contentID]; } else { trace("[ChromeLoader: ] loader by this ID not found!"); return null; //return new MovieClip(); //uncomment if you don't want to get RuntimeError } } //return movie clip (avm1/avm2) for given ID public function getMovieClip(_clipID:Number):MovieClip{ if(_loaders[_clipID]){ return _loaders[_clipID].content as MovieClip } else { trace("[ChromeLoader: ] MovieClip by this ID not found!"); return null; //return new MovieClip(); //uncomment if you don't want to get RuntimeError } } //Unloads the loaders, clears the static holders public function clear():void{ try{ _content.splice(0, _content.length); //clean the dictionary, otherwise this may conflict on reload for(var i:Number = 0; i< _loaderNames.length; i++){ delete( _contentNames[_loaderNames[i]] ) } //clean the loaders for each(var loader:Loader in _loaders){ //1 || removes all listeners loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete); loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError); loader.contentLoaderInfo.removeEventListener(IOErrorEvent.NETWORK_ERROR, onError); //2 || removes it from stage if (loader.parent) { loader.parent.removeChild(loader); } //3 || unload content if (loader.hasOwnProperty("unloadAndStop")) { //fp10 loader.unloadAndStop(true); } else { loader.unload(); } //removes it from memory for all loader = null; } System.gc(); } catch(error:Error){ trace("[ChromeLoader error on destroy: ] " + error.message); } } //// PRIVATE private function loadContent(_cleanUrl:String):void{ _maxTries = 0; var contentLoader:Loader = new Loader(); contentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true); contentLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true); contentLoader.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onError, false, 0, true); contentLoader.load( new URLRequest( _site + _cleanUrl )); trace("loading: " + String(_loadersCompleted+1) + " out of " + _loaderNames.length + " | " + _site + _cleanUrl); } private function onComplete(event:Event):void { _loaders.push(event.target.content.parent as Loader); _content.push( event.target.content.parent ); _contentNames[String(_loaderNames[_loadersCompleted])] = event.target.content.parent as Loader; _loadersCompleted++; if(_loadersCompleted < _loaderNames.length){ loadContent(_loaderNames[_loadersCompleted]); } else { _finished = true; dispatchEvent(new Event(Event.COMPLETE)); } } private function onError(event:Event):void{ _maxTries++; if(_maxTries < 3){ var contentLoader:Loader = new Loader(); contentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true); contentLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true); contentLoader.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onError, false, 0, true); contentLoader.load( new URLRequest( _backupUrl + _loaderNames[_loadersCompleted] )); trace("[ERROR] on loading " + _loaderNames[_loadersCompleted] + " .Loading backup file: " + _backupUrl + _loaderNames[_loadersCompleted]); } else { trace("[ChromeLoader: ] on Error error - max tries reached! Load never completed!"); } //removes the listeners from failed loader, it is ready for Garbage Collection (event.target as LoaderInfo).removeEventListener(Event.COMPLETE, onComplete); (event.target as LoaderInfo).removeEventListener(IOErrorEvent.IO_ERROR, onError); (event.target as LoaderInfo).removeEventListener(IOErrorEvent.NETWORK_ERROR, onError); } //// PUBLIC STATIC //returns DisplayObject for given ID (the ID is the index position of the item in the loading names array) public static function getContent(_contentID:Number):DisplayObject{ if(_content[_contentID]){ return _content[_contentID]; } else { trace("[ChromeLoader: ] Content with this ID( " + _contentID + " )not found!"); return null; //return new MovieClip(); //uncomment if you don't want to get RuntimeError } } //get content for given name, just pass name from the array you loaded public static function getContentByName(_contentName:String):DisplayObject{ if(_contentNames[_contentName]){ return _contentNames[_contentName]; } else { trace("[ChromeLoader: ] Content with this NAME( " + _contentName + " )not found!"); return null; //return new MovieClip(); //uncomment if you don't want to get RuntimeError } } //// GETTERS / SETTERS //returns true if the loader have loaded all images public function get finished():Boolean{ return _finished; } }//end }