title: AS3 Loading multiple external images into an array?
body: <p>Trying to pick 4 images randomly and load them into and array and then show them using a timer when all 4 images are shown 4 more images will be loaded again. here is the code:</p>
  
  <pre><code>var images : Array = new Array();
  var rndNumbers : Array = new Array();
  var imageLoader : Loader;
  var imageTimer : Timer = new Timer(3000, 0);
  var currImageID : int;
  var imgID : int;
  var loaded : Boolean = true;
  var i : int;
  var tmp : int = 0;
  var rnd : int = 0;
  
  addEventListener(Event.ENTER_FRAME, OnLoad);
  
  imageTimer.addEventListener(TimerEvent.TIMER, ChangePicture);
  
  function OnLoad(e : Event) : void {
      RandomNumbers();
      LoadImages();
  }
  
  function RandomNumbers() {
      for (var n = 0; n &lt;= 3; n++) {
          rnd = 1 + Math.floor(Math.random() * 4);
          while (tmp == rnd) {
              rnd = 1 + Math.floor(Math.random() * 4);
          }
          tmp = rnd;
          rndNumbers[n] = rnd;
          trace(rnd);
      }
  }
  
  function LoadImages() : void {
      for (var i = 0; i &lt;= rndNumbers.length - 1; i++) {
          imageLoader = new Loader;
          var urlRequest : URLRequest = new URLRequest("images/pic" + rndNumbers[i] + ".jpg");
          imageLoader.load(urlRequest);
          imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadComplete);
      }
      imageTimer.start();
  }
  
  function LoadComplete(event : Event) {
      removeEventListener(Event.ENTER_FRAME, OnLoad);
      images[imgID] = imageLoader;
      imgID++;
  }
  
  function ChangePicture(event : TimerEvent) : void {
      transition.gotoAndPlay(1);
      Img_Box.addChild(images[currImageID]);
      if (currImageID != 3) {
          currImageID++;
      } else {
          RandomNumbers();
          LoadImages();
          currImageID = 0;
      }
  }
  </code></pre>
  
  <p>The possible problem here would be that the for loop which loads images won't wait for the load to complete and continues the loop whatsoever. How is can that be solved? Or do you suggest any better way?</p>
  
  <p>Thanks in advanced.</p>