Search code examples
actionscript-3flashvariablesloopsdynamic

AS3 creating dynamic Loader Names in a loop


EDITED for clarity: I want to load all kinds of images from an external url in a for loop. I want to call them in a loop and when it's over I start the loop again. however I don't want to call them again with an "http request" rather I would like to loop through the loaded images over and over again.

Is it possible to create a dynamic Loader name in a loop?

Here is the code example:

var Count = 0;
// Loop Begins
var Count:Loader = new Loader();
Count.load(new URLRequest("myurl");
addChild(Count);
Count++;
// End Loop

Another example would be to have a NAME and just add the number on the end. But how

var Count = 0;
// Loop Begins
var MyName+Count:Loader = new Loader(); 
MyName+Count.load(new URLRequest("myurl");
addChild(MyName+Count);
Count++;
// End Loop

IN SHORT: I want to load a bunch of images into an Array and loop through the loaded images by calling them later.

Thanks so much!


Solution

  • Now that we're on the same page:

    var imgArray:Array = new Array;
    var totalImages:int = 42;
    var totalLoaded:int = 0;
    var loaded:Boolean = false;
    
    function loadImages():void{
      for(var count:int = 0; count < totalImages; count++){
        var image:Loader = new Loader();
        image.load(new URLRequest("image" + i + ".jpg");
        image.addEventListener(Event.COMPLETE, loaded);
        imgArray.push(image);
      }
    }
    
    function loaded(e:Event):void{
      totalLoaded++;
      if (totalLoaded == totalImages){
        loaded = true;
      }
    }
    
    function displayImages():void{
      if (loaded){
        for(var i:int = 0; i < imgArray.length(); i++){
          addChild(imgArray[i]);
        }
      }
    }