AS3 Listeners — Loader.COMPLETE vs MovieClip.ENTER_FRAME

Trying to load a directory of hundreds of images into separate movie clips using the AS3 Loader class. By assigning an Event.COMPLETE listener to the Loader, the callback function only has a reference to the Loader and not to the image that’s being loaded or the parent that it’s being loaded into. So if I need to Tween on this image after the load is completed, no dice. Instead, assign an Event.ENTER_FRAME to the parent MovieClip and dig a byte comparison out of the contentLoaderInfo object…

 

for each ( var imgPath in imgXML.elements () ) {
	var imgLoader:Loader = new Loader ();
	imgClip.addChild ( imgLoader );
	imgClip.addEventListener ( Event.ENTER_FRAME, imgLoading );
	imgLoader.load ( new URLRequest ( imgPath ) );
}

function imgLoading ( e:Event ):void {
	var bL:Number = e.target.getChildAt ( 0 ).contentLoaderInfo.bytesLoaded;
	var bT:Number = e.target.getChildAt ( 0 ).contentLoaderInfo.bytesTotal;
	if ( bT == bL ) {
		e.target.removeEventListener ( Event.ENTER_FRAME, imgLoading );
		e.target.parent.Tweener // etc.
	}
}


AS3 Naming and Retrieving Elements Dynamically

Recently ran across this problem in Actionscript 3 with dynamically naming and retrieving child elements. Created a loop to name sprites and add using the name property and addChild method:

for ( var i:int = 1;  i <= 3;  i++ ) {
	var bkgd:Sprite = new Sprite ();
	bkgd.name = "bkgd" +i;
	addChild ( bkgd );
}

 

Now these sprites are accessible via getChildByName and are retrieved as DisplayObject by default. But when retrieved as DisplayObject, you can not add children via addChild method. The trick is to cast the DisplayObject as a Sprite:

var addBkgd:Sprite = Sprite ( this.getChildByName( "bkgd" +layerNum ) );
addBkgd.addChild ( layerSprite );

 

hexbkgd