thenk you guys and Andrei1 specially.
i knew it is possible to set width and height in complete event function. here is brief:
i've a tile list component on my stage which shows some images from over 1000 images. whenever it is selected or changed, it fires a function (let say tileChange).
this function basically defines few variables:
var loaders:Array = [new Loader(), new Loader(), new Loader(), new Loader(), new Loader(), new Loader()];
var loadersURL:Array = [new URLRequest(), new URLRequest(), new URLRequest(), new URLRequest(), new URLRequest(), new URLRequest()];
var loadersSize:Array = ["", "", "", "", "", ""];
var loadersFunction:Array = [function1, function2, function3, function4, function5, function6];
var loaded:Array = ["no", "no", "no", "no", "no", "no"];
now this functions check what is currently selected.
var curSel:String = tileListComp.selectedItem.label;
and than it assigns proper image path of loadersURL. (i m skipping all the calculation).
... loadersURL = path; ...
once loadersURL is assigned, it checks which loaderURL's element is still null, it skips that element and rest of elements are being loaded by loaders variable.
immidiately it adds COMPLETE eventListener which sets the size of loaded image.
for (var i:uint = 0; i < loadersURL.length; i++)
{
if (loadersURL.url != null)
{
loaders.load(loadersURL;
loaders.contentInfo.addEventListener(Event.COMPLETE, loadersFunction);
}
}
//now here, all the loaders are loaded and i want actionscript to wait untill all the loaders are completely loaded. once they're loaded, the rest will continue. i was so confused and i struggled for 2 days. finally what i did is i set timer here and check if all the elements of loaded array have value "Loaded" and if true and to go ahead..
function function1(e:Event):void
{
//sets the size;
loaded[0] = "Loaded";
}
what do you say Andrei1 how is that??
I think this flow is quite inefficient and, most of all, expensive from memory consumption standpoint. Also this approach is quite inflexible.
Here is a code that accomplishes loading of practically unlimited number of images. The only thing to do is to populate imageURLs with appropriate urls. It may not fit all the needs but because references to loaded images are stored in one place - you can always find them at runtime:
var imagURLs:Array = ["img0.jpg", "img1.jpg", "img2.jpg", "img3.jpg", "img4.jpg"];
// array of objects
var images:Array = [];
var loadCounter:int = 0;
loadImages();
function loadImages():void {
var loader:Loader;
for (var i:int = 0; i < images.length; i++) {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
loader.load(new URLRequest(images));
images.push({loader: loader, index: i});
}
}
function onLoadComplete(e:Event):void
{
LoaderInfo(e.target).removeEventListener(Event.COMPLETE, onLoadComplete);
loadCounter++;
if (loadeCounter == imgURLs.length) setSizes();
}
function setSizes():void
{
var image:Bitmap;
for each(var obj:Object in images) {
image = obj.loader.content;
image.width = 200;
image.height = 100;
obj.image = image;
}
}