Loader object does not dispatch Event.COMPLETE

I use this class to upload multiple images at once. Somehow, the bootloader does not fire any event (Event.COMPLETE, ProgressEvent.PROGRESS), strange I also do not get any errors (using FlashDevelop and Flex3 SDK).

package  
{
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.URLRequest;

    public class MultiImgLoader extends EventDispatcher
    {
        private var img_array:Array;
        public var images:Array;
        private var loader:Loader = new Loader();

        public function MultiImgLoader(img_array:Array) 
        {
            this.img_array = img_array;
            trace("[MultiImgLoader] about to load " + img_array.length);
            if (img_array.length > 0)
            {
                load(img_array[0]);
            }
        }

        private function load(img:String):void
        {
            trace("[MultiImgLoader] load " + img);
            loader.addEventListener(ProgressEvent.PROGRESS, progress);
            loader.addEventListener(Event.COMPLETE, this.ready);
            var req:URLRequest = new URLRequest(img);
            loader.load(req);
        }

        public function ready(ev:Event):void
        {
            var key:String = ev.target.contentLoaderInfo.url;
            trace("[MultiImgLoader] ready " + key);
            images.push( { key : ev.target } );
            if (img_array.length > images.length)
            {
                for (var i:int = 0; i < img_array.length; i++ )
                {
                    if (img_array[i] == key)
                    {
                        load(img_array[i+1]);
                    }
                }
            }
        }

        public function progress(ev:ProgressEvent):void
        {
            trace(ev.bytesLoaded);
        }

    }

}
+3
source share
3 answers

Ok, got it. It:

loader.addEventListener(ProgressEvent.PROGRESS, progress);
loader.addEventListener(Event.COMPLETE, this.ready);

read the following:

loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, ready);

I don’t even know why the Loader-Class has an addEvenListener method - any redundancy?

+6
source

I had the same problem with a weak listener, normal work is fine

//BUG event not fired
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler,false,0,true);  

//OK event fired
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);   
+3
source
+1

Source: https://habr.com/ru/post/1711159/


All Articles