AS3/PV3D Loading Data

With actionscript3 comes alot of new changes. One of the most often used features of flash is importing/loading and dealing with external data. In actionscript2 you could use a movieClipLoader, or a simple load to achieve basically what you needed to do. With AS3, you have a little more control over the data that is being parsed, and what happens before, during, and after loading.

The first major change for AS3, is that you need to specify a request using AS3’s new URLRequest datatype and function. This is done by specifying a connection string something like this:

var request:URLRequest = new URLRequest("comp"+startI+".swf");

From this code, you can tell that some sort of dynamic, external swf data is being loaded in.

After your request is setup, the next thing to do is setup the actual loader. This is done like this:

var loader:Loader = new Loader();

Once your loader is setup, you can setup listeners for your loader. The listeners are the important part, as they can trigger different phases of your load progress. For this example, ill use a progress complete event, and a load-in-progress event.

loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,progressHandler);

The really important thing here is that you NEED to define the progress events before you make the call to the load or else it will not run properly, and it will not recognize your events. The final line of the example is just to go ahead and call the load.

loader.load(request);

The two defined events can be setup as functions if you like, to determine percentage loaded, etc. An example percentage loaded function would look something like this-

function progressHandler(evt:ProgressEvent) {
var percentage  = (evt.bytesLoaded/evt.bytesTotal)*100;
}

There you have it, the actionscript3 way of loading external data. I use this method constantly for loading dynamic textures for papervision projects, or to simply parse XML, it all works the same way.

About Curious Minds
We are a web development firm in New York and Chicago, providing development resources and consulting for websites and mobile apps since 2004.