One instance of Loader per DisplayObject, how scary?

I was doing some tests a few days ago, prompted by Darren Richardson and we discovered to our horror that the an instance of the AS 3.0 Loader class can only be used to load one display object. It just seems so wrong. The test was to have one instance of the Loader load its contents into the two Image instances - horror of horrors it only loaded into one Image. Its sort of tantamount to saying that an object can only provide its data for 1 designated class. Surely the fact that the Loader class is a separate object should make its data accessible to any classes requesting its service. Apparently this had been made so because a single DisplayObject may not be added to more than one parent. To achieve success multiple instances of the Loader had to be used.

Here is the code:

package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.*;
import flash.net.URLRequest;

public class testLoader extends Sprite
{
private var imageArray:Array = new Array("image1.jpg","image1.jpg","image1.jpg","image1.jpg");
private var loader:Loader = new Loader();
private var loader1:Loader = new Loader();
private var imgCnt:int = 0;

public function testLoader()
{
trace ("Loader")
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader1.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
var mov1:Sprite = new Sprite()
var mov2:Sprite = new Sprite()
mov1.addChild(loader);
mov2.addChild(loader);
mov2.y = 200
addChild(mov1);
addChild(mov2);
loader.load(new URLRequest(imageArray[imgCnt]));

}

public function completeHandler(evt:Event):void
{
if (imgCnt <>
{
trace("loaded", evt, imgCnt);
loader.load(new URLRequest(imageArray[imgCnt]));
loader1.load(new URLRequest(imageArray[imgCnt]));
imgCnt++
}

}
}
}

0 comments:

My Instagram