Skip to main content
Inspiring
October 7, 2010
Answered

simple but damn stuck - wait for the images to load

  • October 7, 2010
  • 3 replies
  • 1499 views

i believe it is quite simple but i'm not able to do this. situation is something like this:

function name123():void //i m already in a function.
{
....
var a:Loader = new Loader(); //define a loader
var b:new URLRequest = new URLRequest(path to the image); //give the path
a.load(b);
a.contentLoaderInfo.addEventListener(Event.COMPLET  E, eventComplete);
//now the codes after this line, i want them to run only after the above loading is finished. because if i'll type below codes, it will not work.
a.width = 100;
a.height = 100;
}

function eventComplete(e:Event):void
{
//codes
}

i know if i give set width and height in eventComplete function, i could do but the situation is different and i've to set the sizes in name123 function only. kindly help me out.. its been two days now...

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

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;
     }
}

3 replies

Participating Frequently
October 7, 2010

function eventComplete(e:Event):void
{
     var loader = e.target.loader;

     loader.x = 100;

     loader.y = 100;
}

Inspiring
October 7, 2010

Just out of curiosity - why do you need to set sizes in name123 function?

Inspiring
October 7, 2010

oh dear, i wrote that just to give you an example. that is how i want. all these thing i've typed here once again. my program is a bit long but i've stuck here.

the scenario is:

I've defined 6 loaders in an array. //that can be either local or global

I've defined 6 URLRequests in an array. //that can be either local or global

I'm in a function right now.

Now I've a "For" loop.

  As per the specifications given previously, I would assign URLRequests to their loaders.

  Each loader would load URLRequests.

  Each loader will have Event.COMPLETE event (that is because I want to set the size of images)

Now Loop is finished.

Here I want to set the size of all of these Loaders (loaders.width & loaders.height) which currently I'm not able to do.

I don't understand what do now... It been more than two days and still I'm struggling to get a solution.

I hope you understand the situation..

Inspiring
October 7, 2010

Again, you cannot set sizes until asset is loaded. So, you need to set sizes within COMPLETE listener.

Inspiring
October 7, 2010

You cannot set sizes for an empty DisplayObject. Sizes are relevant ONLY when display list has children.