Loading Multiple Images using the gpUrlLoader Class

I discussed the use of the gpUrlLoader Class in a previous post (see: AS3: A URLLoader Class). This class, part of my gpAS3Library, will load image, swf, text, xml, html, stylesheet, json, and sound files.

In this post, I will discuss how to use the gpUrlLoader class to load multiple images into a Flash movie. I used this technique when I created the gpFlashGallery (A free Flash/XML photo gallery).

?View Code ACTIONSCRIPT
package {
 
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.events.*;
	import com.grasshopper.utils.*;
 
	public class gpGallery extends Sprite {
                private var images_xml:XMLList;
		private var images_ar:Array = new Array();
		private var cnt:int = 0;
                private var total_images:int;
 
		public function gpGallery() {
 
		}
 
		public function init(images:XML):void {
                        images_xml = images;
                        total_images = images_xml.length();
			loadImage();
		}
 
                private function loadImage():void  {
			images_ar[cnt] = new gpUrlLoader();
			images_ar[cnt].addEventListener(Event.COMPLETE, imageLoaded);
			images_ar[cnt].init(images_xml[cnt].@thumb, 'image');
		}
 
                private function imageLoaded(e:Event):void {
			var ldr:Loader = images_ar[cnt].getImage();
                        addChild(ldr);
                        // ldr.x = 
                        // ldr.y =
			cnt++;
			if (cnt < total_images) {
				loadImage();
			} 
		}
}

I store each instance of the gpUrlLoader in an array.

?View Code ACTIONSCRIPT
images_ar[cnt] = new gpUrlLoader();

I initialize the gpUrlLoader class using a thumb attribute from an images XMLList.

?View Code ACTIONSCRIPT
images_ar[cnt].init(images_xml[cnt].@thumb, 'image');

Once the image is loaded, I call the getImage method of the gpUrlLoader class.

?View Code ACTIONSCRIPT
var ldr:Loader = images_ar[cnt].getImage();

If the count (cnt) is less than the total number of images, call the loadImage method.

?View Code ACTIONSCRIPT
if (cnt < total_images) {
	loadImage();
}

That’s it. Loading multiple images into a flash movie has never been so easy.

Be Sociable, Share!

Checkout My New Site - T-shirts For Geeks