Skip to content

Instantly share code, notes, and snippets.

@dcloud
Created October 25, 2010 20:58
Show Gist options
  • Save dcloud/645746 to your computer and use it in GitHub Desktop.
Save dcloud/645746 to your computer and use it in GitHub Desktop.
Example of using Loader class to load images, plus some stuff on tracing items in a DisplayObjectContainer's display list.
package {
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.*;
public class ImageLoader extends Sprite
{
private var _ldr:Loader;
private var _urlReq:URLRequest;
private var _imgUrl:String = 'image/birdy.jpg';
/*
DisplayObjects created in fla
-----------------------------
_imgDisplay_mc:MovieClip;
*/
public function ImageLoader()
{
// construct up our loader object
_ldr = new Loader();
_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
_ldr.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, trackLoadProgress);
// construct our URL request object
_urlReq = new URLRequest(_imgUrl);
_ldr.load(_urlReq);
}
private function trackLoadProgress(pEvent:ProgressEvent) {
trace("progressHandler: bytesLoaded=" + pEvent.bytesLoaded + " bytesTotal=" + pEvent.bytesTotal);
}
private function onComplete(evt:Event):void
{
trace("Load Complete");
/*
Next, add our loader object to the display list, or
the loader object's content property,
which points to the DisplayObject that has the image
– – – – – – – – –
| – – – – – – – | <---- Loader (a DisplayObjectContainer)
| |#############| |
| |#############| |
| |#############|<------ content (a DisplayObject)
| |#############| |
| – – – – – – – |
– – – – – – – – –
*/
_imgDisplay_mc._innerArea_mc.addChild(_ldr); // OR this.addChild(_ldr.content);
/* What are the properties of the _ldr? of _ldr.content? Use the DEBUGGER */
/*
What about looking at the display list? Debugger isn't
helpful with this, so we write some code to
show children of _imgDisplay_mc
*/
trace('_imgDisplay_mc has ' + _imgDisplay_mc.numChildren + ' children');
for (var i:int = 0; i < _imgDisplay_mc.numChildren; i++)
{
trace( _imgDisplay_mc.getChildAt(i).name );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment