Posted By

burnandbass on 12/21/11


Tagged


Versions (?)

LightLoader - load array of images, GC, bug fixes, 1.2k compressed


 / 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

  1. /*
  2. Author : Chrysto Dimchev ( burnandbass [at] gmail [.] com )
  3. */
  4. package {
  5.  
  6. import flash.display.MovieClip;
  7. import flash.display.DisplayObject;
  8. import flash.display.Loader;
  9. import flash.display.LoaderInfo;
  10. import flash.events.EventDispatcher;
  11. import flash.events.Event;
  12. import flash.events.ProgressEvent;
  13. import flash.events.IOErrorEvent;
  14. import flash.net.URLRequest;
  15. import flash.utils.Dictionary;
  16. import flash.system.System;
  17.  
  18. public class ChromeLoader extends EventDispatcher {
  19.  
  20. private static var _content:Array = [];
  21. private static var _contentNames:Dictionary = new Dictionary(true);
  22.  
  23. private var _loadersCompleted:Number = 0;
  24. private var _loaders:Array = [];
  25. private var _loaderNames:Array = [];
  26. private var _maxTries:Number = 0;
  27.  
  28. private var _site:String = "";
  29. private var _backupUrl:String = "";
  30.  
  31. private var _finished:Boolean = false;
  32.  
  33. /* CONSTRCTOR
  34. pass params:
  35. @site --> url to prepend the loaders (expl: "http://site.com/images")
  36. @backupURL --> backup url, the loader will try to load the same picture, but with this string instead ot @site (expl: http://sitebackup.com/images)
  37. */
  38. public function ChromeLoader(site:String = null, backupURL:String = null):void {
  39. if(site){ _site = site }
  40. if(backupURL){ _backupUrl = backupURL }
  41. }
  42.  
  43. //// PUBLIC
  44.  
  45. //load images/MovieClips, pass array of items to load
  46. public function loadContents(_items:Array):void{
  47. for(var i:Number = 0; i< _items.length; i++){
  48. _loaderNames.push( _items[i] );
  49. }
  50.  
  51. loadContent(_loaderNames[_loadersCompleted]);
  52. }
  53.  
  54. //returns loader for given ID
  55. public function getLoader(_contentID:Number):Loader{
  56. if(_loaders[_contentID]){
  57. return _loaders[_contentID];
  58. } else {
  59. trace("[ChromeLoader: ] loader by this ID not found!");
  60. return null;
  61. //return new MovieClip(); //uncomment if you don't want to get RuntimeError
  62. }
  63. }
  64.  
  65. //return movie clip (avm1/avm2) for given ID
  66. public function getMovieClip(_clipID:Number):MovieClip{
  67. if(_loaders[_clipID]){
  68. return _loaders[_clipID].content as MovieClip
  69. } else {
  70. trace("[ChromeLoader: ] MovieClip by this ID not found!");
  71. return null;
  72. //return new MovieClip(); //uncomment if you don't want to get RuntimeError
  73. }
  74. }
  75.  
  76. //Unloads the loaders, clears the static holders
  77. public function clear():void{
  78. try{
  79. _content.splice(0, _content.length);
  80.  
  81. //clean the dictionary, otherwise this may conflict on reload
  82. for(var i:Number = 0; i< _loaderNames.length; i++){
  83. delete( _contentNames[_loaderNames[i]] )
  84. }
  85.  
  86. //clean the loaders
  87. for each(var loader:Loader in _loaders){
  88. //1 || removes all listeners
  89. loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
  90. loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onError);
  91. loader.contentLoaderInfo.removeEventListener(IOErrorEvent.NETWORK_ERROR, onError);
  92. //2 || removes it from stage
  93. if (loader.parent) {
  94. loader.parent.removeChild(loader);
  95. }
  96. //3 || unload content
  97. if (loader.hasOwnProperty("unloadAndStop")) { //fp10
  98. loader.unloadAndStop(true);
  99. } else {
  100. loader.unload();
  101. }
  102.  
  103. //removes it from memory for all
  104. loader = null;
  105. }
  106.  
  107. System.gc();
  108.  
  109. } catch(error:Error){
  110. trace("[ChromeLoader error on destroy: ] " + error.message);
  111. }
  112. }
  113.  
  114. //// PRIVATE
  115.  
  116. private function loadContent(_cleanUrl:String):void{
  117. _maxTries = 0;
  118. var contentLoader:Loader = new Loader();
  119. contentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
  120. contentLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
  121. contentLoader.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onError, false, 0, true);
  122. contentLoader.load( new URLRequest( _site + _cleanUrl ));
  123. trace("loading: " + String(_loadersCompleted+1) + " out of " + _loaderNames.length + " | " + _site + _cleanUrl);
  124. }
  125.  
  126. private function onComplete(event:Event):void {
  127. _loaders.push(event.target.content.parent as Loader);
  128.  
  129. _content.push( event.target.content.parent );
  130.  
  131. _contentNames[String(_loaderNames[_loadersCompleted])] = event.target.content.parent as Loader;
  132.  
  133. _loadersCompleted++;
  134.  
  135. if(_loadersCompleted < _loaderNames.length){
  136. loadContent(_loaderNames[_loadersCompleted]);
  137. } else {
  138. _finished = true;
  139. dispatchEvent(new Event(Event.COMPLETE));
  140. }
  141. }
  142.  
  143. private function onError(event:Event):void{
  144. _maxTries++;
  145. if(_maxTries < 3){
  146. var contentLoader:Loader = new Loader();
  147. contentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete, false, 0, true);
  148. contentLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
  149. contentLoader.contentLoaderInfo.addEventListener(IOErrorEvent.NETWORK_ERROR, onError, false, 0, true);
  150. contentLoader.load( new URLRequest( _backupUrl + _loaderNames[_loadersCompleted] ));
  151. trace("[ERROR] on loading " + _loaderNames[_loadersCompleted] + " .Loading backup file: " + _backupUrl + _loaderNames[_loadersCompleted]);
  152. } else {
  153. trace("[ChromeLoader: ] on Error error - max tries reached! Load never completed!");
  154. }
  155.  
  156. //removes the listeners from failed loader, it is ready for Garbage Collection
  157. (event.target as LoaderInfo).removeEventListener(Event.COMPLETE, onComplete);
  158. (event.target as LoaderInfo).removeEventListener(IOErrorEvent.IO_ERROR, onError);
  159. (event.target as LoaderInfo).removeEventListener(IOErrorEvent.NETWORK_ERROR, onError);
  160. }
  161.  
  162. //// PUBLIC STATIC
  163.  
  164. //returns DisplayObject for given ID (the ID is the index position of the item in the loading names array)
  165. public static function getContent(_contentID:Number):DisplayObject{
  166. if(_content[_contentID]){
  167. return _content[_contentID];
  168. } else {
  169. trace("[ChromeLoader: ] Content with this ID( " + _contentID + " )not found!");
  170. return null;
  171. //return new MovieClip(); //uncomment if you don't want to get RuntimeError
  172. }
  173. }
  174.  
  175. //get content for given name, just pass name from the array you loaded
  176. public static function getContentByName(_contentName:String):DisplayObject{
  177. if(_contentNames[_contentName]){
  178. return _contentNames[_contentName];
  179. } else {
  180. trace("[ChromeLoader: ] Content with this NAME( " + _contentName + " )not found!");
  181. return null;
  182. //return new MovieClip(); //uncomment if you don't want to get RuntimeError
  183. }
  184. }
  185.  
  186. //// GETTERS / SETTERS
  187.  
  188. //returns true if the loader have loaded all images
  189. public function get finished():Boolean{
  190. return _finished;
  191. }
  192.  
  193. }//end
  194. }

Report this snippet  

You need to login to post a comment.