1

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!

2
  • It might help me understand your question if you explain why you want to go back into that for loop. Commented Aug 9, 2012 at 4:43
  • You should have a look to LoaderMax. Commented Aug 9, 2012 at 9:04

2 Answers 2

2

CASE1 code is how to load images in Sequence.

CASE2 code is how to load images in Synchronized.

First, the URL you want to import all images must be named sequentially. for example Must be in the following format:

www.myURL.com/img0.jpg 
www.myURL.com/img1.jpg 
www.myURL.com/img2.jpg 
www.myURL.com/img3.jpg 
www.myURL.com/img4.jpg 
www.myURL.com/img5.jpg
        .
        .
        .

Try the code below, just a test.

CASE1

var   imgLoader:Loader;
var   imgRequest:URLRequest;
var   count:int = -1;
const TOTAL_COUNT:int = 10;
var   imgBox:Array = [];
var   isCounting:Boolean;

function loadImage():void
{
    count ++;

    isCounting = true;

    imgLoader = new Loader();
    imgRequest = new URLRequest();
    imgRequest.url = "www.myURL.com/img" + count +".jpg";
    imgLoader.load(imgRequest);
    imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
    imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);

    if(count == TOTAL_COUNT)
    {
          isCounting = false;
          count = -1;
    }
}

function onLoadedImg(e:Event):void
{
    imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadedImg);

    var bmp:Bitmap = e.currentTarget.content;
    bmp.x = Math.random() * width;
    bmp.y = Math.random() * height;
    bmp.width = 100;
    bmp.height = 100;
    this.addChild(bmp);

    imgBox.push(bmp);

    if( isCounting == false) 
    {
        return;
    }

    loadImage();
}

function unloadedImg(e:IOErrorEvent):void
{
    imgLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
    trace("load Failed:" + e);
}

loadImage();

CASE2

var   imgLoader:Loader;
var   imgRequest:URLRequest;
const TOTAL_COUNT:int = 10;
var   imgBox:Array = [];

function loadImage2():void
{
    for(var i:int = 0; i<TOTAL_COUNT; i++)
    {
        imgLoader = new Loader();
        imgRequest = new URLRequest();
        imgRequest.url = "www.myURL.com/img" + i +".jpg";
        imgLoader.load(imgRequest);
        imgLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
        imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadedImg);
    }
}

function onLoadedImg(e:Event):void
{
    imgLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadedImg);

    var bmp:Bitmap = e.currentTarget.content;
    bmp.x = Math.random() * width;
    bmp.y = Math.random() * height;
    bmp.width = 100;
    bmp.height = 100;
    this.addChild(bmp);

    imgBox.push(bmp);
}

function unloadedImg(e:IOErrorEvent):void
{
    imgLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, unloadedImg);
    trace("load Failed:" + e);
}

loadImage2();

If you want access loaded Image. refer a following code. If you do not put them in an array can not be accessed in the future.

for(var int:i=0; i<TOTAL_COUNT; i++)
{
    var bitmap:Bitmap = imgBox[i] as Bitmap;
    trace("index: " + i + "x: " + bitmap.x + "y: " + bitmap.y, "width: " + bitmap.width + "height: " + bitmap.height);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I love this idea. It's a bit over my head tho. :( I would really love to know if I can load images into an array and then do an "addChild(DynamicName)" later. Make sense? My other script is a bit complex on how I get the dynamic vars.
The reason I containing an array is to reuse. After you create an object exists in memory. However, Do not know where they are. so, push them in an array. And later when accessing imgBox[0], imgBox[1] If this is used.
1

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

5 Comments

But is there a way to make dynamic loader names? Because I need to call the loader later. It would be like addChild(ImgArray[i]) etc..
I'm sorry, I edited my code, I had pushed count instead of image. You should also instantiate your Loader outside of the for..loop. (var image:Loader = new Loader();"
Basically I don't want to addChild() in the first loop. I want to get all the images loaded in to "some sort of Array" Then I can load the images later and loop through the array multiple times. This code would load the image each time new. I need to load it from an array that has already been loaded. Make sense? thanks for your efforts
@PapaDeBeau I added a bit more for you. this loads all the images into an array. When you are ready to add your images in, you can do so by calling displayImages(). You can manipulate them with imgArray[i].x = 10; for instance. Let me know if you have any questions with any of the code.
I figured it out using bits of the answer you gave me. I will post my code later. Thanks so much for all your effort @Duke. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.