Loading content dynamically
You can load any of the following external display assets into an ActionScript 3.0 application:
A SWF file authored in ActionScript 3.0—This file can be a Sprite, MovieClip, or any
■
class that extends Sprite.
An image file—This includes JPG, PNG, and GIF files.
■
An AVM1 SWF file—This is a SWF file written in ActionScript 1.0 or 2.0.
■
You load these assets by using the Loader class.
The Loader class
Loader objects are used to load SWF files and graphics files into an application. The Loader
class is a subclass of the DisplayObjectContainer class. A Loader object can contain only one
child display object in its display list—the display object representing the SWF or graphic file
that it loads. When you add a Loader object to the display list, as in the following code, you
also add the loaded child display object to the display list once it loads:
var pictLdr:Loader = new Loader();
var pictURL:String = "banana.jpg"
var pictURLReq:URLRequest = new URLRequest(pictURL);
pictLdr.load(pictURLReq);
this.addChild(pictLdr);
Once the SWF file or image is loaded, you can move the loaded display object to another
display object container, such as the
example:
import flash.display.*;
import flash.net.URLRequest;
import flash.events.Event;
var container:Sprite = new Sprite();
addChild(container);
var pictLdr:Loader = new Loader();
var pictURL:String = "banana.jpg"
var pictURLReq:URLRequest = new URLRequest(pictURL);
pictLdr.load(pictURLReq);
pictLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
function imgLoaded(e:Event):void
{
container.addChild(pictLdr.content);
}
DisplayObjectContainer object in this
container
Basics for working with the core display classes
179