0
votes

I am having this error, not sure why it appears, i believe its something with scope problem. Because i have added Child already. I can't even remove Child, the child is added within the loop, and i am not sure how to manipulate the childs that were added within the loop from the outside(after the loop has ended).

I am trying to add the child from within the loop to a viewport, but i am demonstrating the problem with removeChild to reduce the complication. Because i am trying to locate the reason of why it is giving me this error, and trying to learn what i should do.

Thanks for your time!

for (var j:int = 5; j < somedata.length; j++) 
                {
                    if(somedata[j]){
                    var myLoader:Loader = new Loader();
                    var image:Bitmap;
                    var url:URLRequest = new URLRequest("http://www.rentaid.info/rent/"+somedata[j]);
                    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded);
                    myLoader.load(url);




                    function onImageLoaded(e:Event):void {
                    image = new Bitmap(e.target.content.bitmapData);

                    var mw:Number = bf.width*2;
                    var mh:Number = bf.height*1.2;   
                        image.y = bf4.y+25;
          /* if you set width and height image same with the stage use this */
                    image.width = mw;
                    image.height = mh;
                    var _contentHolder: Sprite;
                    addChild(_contentHolder);
                    _contentHolder.addChild(image);
                    }
            }

                    removeChild(_contentHolder);
            }

Edit:

function LoadImages() : void {
    for (var j:int = 5; j < somedata.length; j++)  {
        var image:Bitmap;
        myLoader = new Loader;
        var urlRequest : URLRequest = new URLRequestfor ("http://www.rentaid.info/rent/"+somedata[j]) 
        myLoader.load(urlRequest);
        myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, LoadComplete);
    }

}

function LoadComplete(event : Event) {

            _contentHolder.addChild(image);
}

Or something like this?

var loadedArray:Array = new Array();
            var counter:int=0;

            function loadImage():void{

             var loader:Loader = new Loader();
                var image:Bitmap;
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,  onImageLoaded);
                loader.load(new URLRequest("http://www.rentaid.info/rent/"+somedata[counter]));






                function onImageLoaded(e:Event):void {
                image = new Bitmap(e.target.content.bitmapData);



                 loadedArray.push(e.target.content);

   if(counter == somedata.length-1){    

       for(var i:uint = 5; i < somedata.length; i++){
           image[i].x = 0 + i * 100;

           addChild(_contentHolder);
            _contentHolder.addChild(image[i]);
                currentY += _contentHolder.height + 10;
       }    
   }

   else{
       counter++;
       loadImage();
1

1 Answers

0
votes

You don't define _contentHolder. You also do not, currently, addChild(_contentHolder). This will be why the removeChild line is throwing an error.
Also, you do not need to addChild(image) and then add image as a child of _contentHolder. One or the other will do. In fact a DisplayObject can only be in ONE place on the display 'tree'. If you want image as a child of _contentHolder only _contentHolder.addChild(image) is necessary.

If you want all of your image instances added to the SAME _contentHolder, it would be best to declare and instantiate _contentHolder OUTSIDE the for loop; Each foor loop iteration will then be adding your images to the same Sprite object. When you remove _contentHolder, the child images will also disappear (although they will still be children of _contentHolder).

Ideally, you should not have more than one Loader operating asynchronously, as you do in your for loop. Instead of using a for loop, create a variable to track which index value you are up to and, when each image loads, increment that value and use it to check if all images are loaded. If not, load the next. This will require the Loader section of your code to be in it's own function, which gets called initially AND from onImageLoaded, if another image needs to be loaded.

The variable that tracks which index you are up to will, likewise, need to be set initially and incremented when each image loads.

In response to your edited question:

Your first new example of code does not attempt to load the NEXT image once the current image is loaded. Also, just as a tip: Function names are not normally written starting with capital letters; This prevents confusion with Class definitions.
Also your first new example of code does not call the 'LoadImages' function to initially load the first image.

Your second example looks like it should work to me. Have you actually tried this version?

The only thing I'd suggest is defining your Loader with the other variables and removing the Event.COMPLETE listener in the 'loaded' function. When adding the event listener you could also add a:

loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress); 

listener and trace out the progress of each image.

I'd also recommend adding trace statements throughout you code to see where it's getting stuck.