Hi,

You have problem with using elementCounter public property.
In showXML event handler you increment the elementCounter in for loop
and then using it as index for the materialArray in ImageLoaded.
Problem is that the loading of images is asynchronous operation and
when the ImageLoaded handler gets called the elementCounter will be
equal to xmlList.length().
One quick solution for this would be to use one tempArray for holding
the references to picLoader.contentLoaderInfo and when the ImageLoaded
gets called you would use tempArray.indexOf( e.target ) as the index
for the materialArray.

Example:

public var imageLoaders:Array = [];
...
public function showXML(e:Event):void
                {
                        xmlData=new XML(e.target.data);
                        xmlList=xmlData.children();
                        xmlListLength=xmlList.length();
                        for (var i:int=0; i < xmlListLength; i++)
                        {
                                var picLoader:Loader=new Loader();
                                picLoader.load(new
URLRequest(xmlList[i].attributes()[0]));
 
picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
ImageLoaded);
 
imageLoaders.push( picLoader.contentLoaderInfo );
                        }
                }

                private function ImageLoaded(e:Event):void
                {
                       var planeMaterial:BitmapMaterial = new
BitmapMaterial(Cast.bitmap(e.target.content));
 
materialArray[imageLoaders.indexOf(e.target)]=planeMaterial;

                       // Removing event handlers is good practice
 
LoaderInfo(e.target).removeEventListener(Event.COMPLETE, ImageLoaded);

                }

On Feb 17, 11:41 am, metSyS <[email protected]> wrote:
> I have some problem. I'm loading some *.xml, get from there some
> attribute in wich I have URL to my pictures. It works correct. After
> it I want to load pictures and write them into array. I think the
> problem is that cycle works faster then images loading. I need to run
> the  cycle and complete the step only when image is loaded and have
> wrote into array. Sorry for my bad english. Need help.
>
> public var elementCounter:int=0;
> ...
> public function showXML(e:Event):void
>                 {
>                         xmlData=new XML(e.target.data);
>                         xmlList=xmlData.children();
>                         xmlListLength=xmlList.length();
>
>                         for (var i:int=0; i < xmlList.length(); i++, 
> elementCounter++)
>                         {
>                                 var picLoader:Loader=new Loader();
>                                 picLoader.load(new 
> URLRequest(xmlList[i].attributes()[0]));
>                                 
> picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
> ImageLoaded);
>                         }
>                 }
>
>                 private function ImageLoaded(e:Event):void
>                 {
>                        var planeMaterial:BitmapMaterial = new
> BitmapMaterial(Cast.bitmap(e.target.content));
>                        materialArray[elementCounter]=planeMaterial;
>                 }

Reply via email to