class flash.flashAS.QueuePreloader extends Object { /** * Private properties * */ private var activeIndex:Number = 0; private var queue:Array; private var stopLoading:Boolean = false; /** * Public properties * */ public var onComplete:Function; public var onProgress:Function; public var onPicture:Function; public var onStop:Function; /** * Constructor * @param [optional] Array with all containers and filenames * */ function QueuePreloader ( queue:Array ) { super(); if ( queue != undefined ) this.initialise ( queue ); } /** * Initialises the object * @param Array with all containers and filenames * */ public function initialise ( queue:Array ):Void { if ( queue != undefined ) { this.queue = queue.slice (); this.loadNextPicture(); } else throw new Error ( "There is no queue." ); } /** * Loads the next picture in queue * */ private function loadNextPicture ():Void { var loader:MovieClipLoader = new MovieClipLoader(); loader.addListener( this); loader.loadClip ( this.queue[ this.activeIndex ].filename, this.queue[ this.activeIndex ].container ); } /** * Executes while loading returning information about its progress * @param Reference to the container in which the picture is loaded * @param The bytes loaded so far * @param The bytes need to be loaded * */ private function onLoadProgress ( container:MovieClip, bytesLoaded:Number, bytesTotal:Number ):Void { var myProgress:Number = Math.round( ( bytesLoaded / bytesTotal ) * 100 ) / 100; var myTotalProgress:Number = Math.round ( ( ( this.activeIndex + myProgress ) / this.queue.length ) * 100 ) / 100; this.onProgress ( myProgress, myTotalProgress ); } /** * Executes if the image is succesfully loaded * @param Reference to the container in which the picture is loaded * */ private function onLoadInit ( container:MovieClip ):Void { this.onPicture ( container ); this.activeIndex++; if ( this.queue.length > this.activeIndex && !this.stopLoading ) this.loadNextPicture (); else if ( this.queue.length > this.activeIndex && this.stopLoading ) this.onStop (); else if ( this.queue.length == this.activeIndex ) this.onComplete (); } /** * Stops the loading of the pictures * */ public function stop ():Void { this.stopLoading = true; } /** * Resumes the loading of the pictures * */ public function resume ():Void { this.stopLoading = false; this.loadNextPicture (); } /** * Executes when an error occured while loading * @param Reference to the container in which the picture is loaded * @param Object representing the error * @param Status of the error * */ private function onLoadError ( container:MovieClip, code:String, status:String ):Void { throw new Error ( "Error while trying to load a picture: " + code ); } }