Load Images and SWFs at Runtime with loadChild()
In standard ActionScript 3.0, loading images or SWFs involves creating a new loader, adding it to the display list, creating a new URLRequest object, and listening for some events. In AS2, it was easy to call loadMovie().
With SimpleAS3, a new function named loadChild() has been added to every display object that can hold children. It automatically creates the Loader object, sets up listeners, and begins the downloading the asset you want to load.
Method Signature
public function loadChild( url:String, name:String = null, onCompleteMethod:Function = null, onProgressMethod:Function = null, onIoErrorMethod:Function = null ):Loader
Parameters
- url : String
The address of the image or SWF to load. May be an absolute or relative URL.
- name : String
value with which to initialize the
nameproperty. The child becomes accessible by passing this value togetChildByName(). If the parent is aMovieClipor another class markeddynamic, the child's name will be added as a property of the parent. This is similar to how the Flash authoring tool declares stage instances as properties. - onCompleteMethod : Function
The method to call when the asset finishes loading. Receives a standard
Eventobject. - onProgressMethod : Function
The method to call periodically while the asset is loading. May be used to display progress information. Receives a
ProgressEventobject. - onIoErrorMethod : Function
The method to call when an error causes the asset to fail loading. Receives an
IOErrorEventobject.
Usage Example
var container = new MovieClip();
this.addChild(container);
var picLoader = container.loadChild( "profilepic.png",
"profilePic", onCompleteHandler );
function onCompleteHandler()
{
var userName = new TextField();
userName.text = "User ID: ExampleGuy";
container.addChild( userName );
userName.y = picLoader.height + 2;
}