Loading external resources into Actionscript 3
Images and flash movies(swf´s) are loaded into flash in a standard way, but when loading more than one image troughout your solution the handeling can be tough to manage in a good way. The standard way in loading resources is by using the Loader class and using the load method with the url as parameter. Here is an example:
var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressLoaded); loader.load(new URLRequest(url));
When I create a movie I allways create a representing class to handle all loading of extenal resourses. The main reason is to not have to add the required methods to handle the events when the resource is loaded. If you create an own class to do this, you get rid of the methods and get a more object oriented view of your code. The class I use is just a simple class, but very effective. Here is the code to do this:
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class LoadExternal extends Sprite
{
private var _url:String = "";
private var _bytesLoadProgress:int = 0;
private var _loaded:Bitmap;
public function LoadExternal(url:String)
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressLoaded);
loader.load(new URLRequest(url));
}
public function onComplete(e:Event):void
{
_loaded = new Bitmap(e.target.loader.contentLoaderInfo.content.bitmapData.clone());
this.addChild(_loaded);
}
public function progressLoaded(e:ProgressEvent):void
{
_bytesLoadProgress = e.bytesLoaded/e.bytesTotal;
}
public function getProsess():int
{
return _bytesLoadProgress;
}
}
}
Hope this little source helps you to be a more effective object oriented actionscripter! Have fun

Hi
Great snippets of code and classes you got here.
I use something similar, but I have a little feature:)
The LoadExternal(url:String) should have a few extra parameters.
By giving it LoadExternal(url:String, success:Function=null, fail:Function=null, progress:Function=null)
In you parent you make three functions, the success(event:Event), the progress(event:Event) and the fail(event:Event) they can, of course, be named whatever, you just pass them to the LoadExternal constructor.
Then you handle all the error, progress, success.. in other words: you handle all the events in you PartyLoader. Upon success, error or progress you just call the functions passed.
This saved you 3 listeners and listenerFunctions in the main code. By setting them to null you can use them or not, use just the success and leave out the progress if you fell like it and so forth.
This has saved me crazy amounts of clutter, I was not used to 100% event driven programming and thinking like this helped me much the last two years.
looks good but how is this how you apply it?
loader:LoadExternal = new LoadExternal;
function loadme():void{
loader(“some/place/thing.jpg”)
}
Hi Ian,
Firstly, create a sprite or a LoadExternal variable like you do in your first line, but remember to use correct programming syntax. The LoadExternal class has a constructor with the url as parameter. The correct usage for this class is:
var image:LoadExternal = new LoadExternal(“some/place/thing.jpg”);
or by using a global variable:
package
{
public class SomeClass …..
private var image:Sprite;
public function SomeClass() {
image = new LoadExternal(“some/place/thing.jpg”);
addChild(image);
}
Hope this helps!
Why does this extend the Sprite class?
The Sprite is extended because I wanted the downloaded media to have a container for which had an relative easy usage on stage. You could also use MovieClip, but Sprites does not have the timeline-extention that MovieClip has and is an easier object to move around if there are a lot of objects. Does this answer your question?
Yes, Thanks!
Great example!
You can add a preloading for external resources?
Thanks…
Hi lillegutt
Need your Help
New in As 3
How utlize this using in flash
after import LoadExternal;
what will be the next step;
Regards
Vishal
Hi Vishal,
The LoadExternal class has a constructor with the url as parameter. The correct usage for this class is:
var image:LoadExternal = new LoadExternal(”some/place/thing.jpg”);
addChild(image); //since the LoadExternal extends Sprite we can add it to stage
or by using a global variable:
package
{
public class SomeClass …..
private var image:Sprite;
public function SomeClass() {
image = new LoadExternal(”some/place/thing.jpg”);
addChild(image);
}
Hope this helps!