/* *********************************************************************** ActionScript 3 Tutorial by Barbara Kaskosz http://www.flashandmath.com/ Last modified: March 19, 2008 ************************************************************************ */ package { import flash.display.*; import flash.events.*; import flash.net.URLRequest; /* We are extending the EventDispatcher class contained in the flash.events package. Any instance of a subclass of EventDispatcher is capable of dispatching custom events. Many of AS3 built-in classes are subclasses of the EventDispatcher class, for example, the Sprite class and other DisplayObjects. */ public class ImageLoader extends EventDispatcher { /* We are defining constants corresponding to our two custom events. Similarly as for built-in events, later, when we add listeners to instances of ImageLoader, we can refer the events by the names of the constants, e.g. ImageLoader.IMGS_LOADED, or by their string value e.g. 'imgsLoaded'. */ public static const IMGS_LOADED:String = "imgsLoaded"; public static const LOAD_ERROR:String = "loadError"; private var loadersArray:Array; private var numImgs:int; private var numLoaded:int; private var isError:Boolean; private var _bitmapsArray:Array; private var loadCanRun:Boolean; public function ImageLoader(){ //The constructor of the class sets the value of 'loadCanRun' variable only. //It is the method 'loadImgs' below that performs all the main tasks. this.loadCanRun=true; } /* 'loadImgs' method takes an array of strings as a parameter. For the method to function properly, the strings should represent addresses of the image files to be loaded. The method listenes for IO loading errors. (For example, the server is too busy and the file appears non-existent.) The method does not listen to FlashPlayer security errors. We assume that the image files are at locations that do not violate the security settings of the swf file that uses ImageLoader. */ public function loadImgs(imgsFiles:Array):void { if(loadCanRun){ loadCanRun=false; //The conter variable counting how many images have been loaded. numLoaded=0; //The variable that remembers the current error status. isError=false; //The number of images to be loaded. numImgs=imgsFiles.length; //The array of bitmaps, each representing a loaded image. _bitmapsArray=[]; /* For each image file, we will use a separete instance of the Loader class. That is because we will be loading images simultaneously rather than consecutively. Consecutive loading is easier to code but it causes visible delays. In the loop that follows, we populate the array of Loaders and attach listeners to each Loader. One listens to an image finishing loading, the other to an occurance of a loading error. Then we evoke the 'load' method for each Loader with the address of the corresponding image. */ loadersArray=[]; for(var i:int=0;i