AS3: Access URLRequest Return Data

I’m creating a new portfolio site using Joomla and Flash. I’ve spent this past weekend creating AS3 class packages (and converting some of my AS2 classes to AS3).

I began loading thumbnails using AS3’s URLRequest and URLLoader in my main application file. I decided to create a package that would request/load the thumbnails, but I needed to return the data from the URLRequest to my main application. I was able to accomplish it using the dispatchEvent method.

Note: This functionality is the basis for my gpUrlLoader class (part of my gpAS3Library).

The URLRequest Package

First I created the URLRequest package.

?View Code ACTIONSCRIPT
package  {
	import flash.display.Sprite;
        import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.net.URLLoader;
	import flash.utils.ByteArray;
	import flash.net.URLLoaderDataFormat;
	import flash.events.*;
 
	public class gpUrlLoader extends Sprite {
		private var urlLoader:URLLoader;
		private var loader:Loader;
		public function gpUrlLoader() {
 
	}
       public function init(url:String):void {
		urlLoader = new URLLoader();
		urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
		urlLoader.load(new URLRequest(url));
		urlLoader.addEventListener(Event.COMPLETE, onComplete);
	}
 
        private function onComplete(e:Event):void {
		loader = new Loader();
		loader.loadBytes(urlLoader.data);
		dispatchEvent(new Event(Event.COMPLETE));
	}
 
        public function getImage():Loader {
		return loader;
	}

So the most important line is the dispacthEvent:

?View Code ACTIONSCRIPT
dispatchEvent(new Event(Event.COMPLETE));

This will the let the environment know when Event.COMPLETE has been fired.

Using the Class

Using the class is simple:

?View Code ACTIONSCRIPT
import gpUrlLoader;
 
var image:gpUrlLoader = new gpUrlLoader();
image.addEventListener(Event.COMPLETE, imageLoaded);
image.init('myimage.jpg');
 
function imageLoaded(event:Event):void {
	var ldr:Loader = image.getImage();
	addChild(ldr);
}

I added an event listener so that I know when the Event.COMPLETE has been fired. Because I used the dispatchEvent method in my gpUrlLoader class, I can listen to the event from any class that calls this class.

When the image has been loaded, I call imageLoaded and which calls the getImage method of the gpUrlLoader class. The getImage method returns the loader itself.

Custom Events

I also had to create a custom event (LOADERROR) for the class. Initially the gpUrlLoader extended a Sprite, but I changed it so that it extended EventDispatcher.

?View Code ACTIONSCRIPT
public class gpUrlLoader extends EventDispatcher { 
 
}

I then created a public static var:

?View Code ACTIONSCRIPT
public class gpUrlLoader extends EventDispatcher { 
        public static var LOADERROR:String = "error";
}

I added an onError event listener:

?View Code ACTIONSCRIPT
public function init(url:String):void {
	urlLoader = new URLLoader();
	urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
	urlLoader.load(new URLRequest(url));
	urlLoader.addEventListener(Event.COMPLETE, onComplete);
        urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onError);
}

The onError methods calls a method that dispatches the custom event:

?View Code ACTIONSCRIPT
private function onError(e:ErrorEvent):void {
     doError();
}
 
public function doError():void {
	dispatchEvent(new Event(LOADERROR));
}

Then in the main app, I add an event listener that listens for a load error:

?View Code ACTIONSCRIPT
ldr.addEventListener(gpUrlLoader.LOADERROR, ioError);
 
function ioError(e:Event):void {
	trace('Error loading file');
}

It is easy to duplicate this functionality or you can download the gpUrlLoader from my gpAS3Library. Enjoy.

Be Sociable, Share!

Checkout My New Site - T-shirts For Geeks