Search code examples
actionscript-3synchronousurlloader

actionscript 3 synchronous loader


I have typical situation where big loop is loading lots of images and its done asynchronous which make browser to frees during loading and I want to make it synchronous but having big trouble doing it. I found this class synchronous loader and it work great but you cant add mouse event listener to loader. Here is sample code:

for (var i = 0; i < items.length; i++) {
        
            item = items[i];
            
            if(item.layer>1){
                ld:Loader = new Loader();
                ld.load(new URLRequest(item.url));
                ld.rotation = item.rotation;
                ld.x = item.x ;
                ld.y = item.y;
                ld.addEventListener(Event.COMPLETE, loadComplete);
                ld.scaleX = item.scaleX;
                ld.scaleY = item.scaleY;
                ld.addEventListener(MouseEvent.MOUSE_DOWN, select);
                layers_arr[item.layer].addChild(ld);
            }

}

Any idea how this can be done?


Solution

  • like arieljake says, here is a little example on how to use it:

    package
    {
        import com.greensock.TweenLite;
        import com.greensock.events.LoaderEvent;
        import com.greensock.loading.ImageLoader;
        import com.greensock.loading.LoaderMax;
        import com.greensock.loading.MP3Loader;
        import com.greensock.loading.SWFLoader;
        import com.greensock.loading.XMLLoader;
        import com.greensock.loading.display.ContentDisplay;
    
        import flash.display.Sprite;
        import flash.display.StageAlign;
        import flash.display.StageScaleMode;
        import flash.events.Event;
    
        public class Main extends Sprite
        {
            public var itemUrl:String;
            public var queue:LoaderMax = new LoaderMax({name:"mainQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});
    
            public function Main()
            {
                stage.align = StageAlign.TOP_LEFT;
                stage.scaleMode = StageScaleMode.NO_SCALE;
    
                queue.maxConnections = 1; //Checks how much items that can be loaded at the same time
                queue.append( new ImageLoader("http://www.myurl.com/myimage.jpg", {name:"photo1", estimatedBytes:2400, container:this, alpha:0,scaleMode:"proportionalInside"}) );
                queue.append( new ImageLoader("http://www.myotherurl.com/awesomeimage.jpg", {name:"photo2", estimatedBytes:2400, container:this, alpha:0, scaleMode:"proportionalInside"}) );
    
                queue.addEventListener(LoaderEvent.CHILD_COMPLETE, childCompleteHandler); //checks when a child has completed to load
                queue.addEventListener(LoaderEvent.CHILD_PROGRESS, childProgressHandler); //checks the child progress
    
                //prioritize the loader named "photo1"
                LoaderMax.prioritize("photo1");  //same as LoaderMax.getLoader("photo1").prioritize();
    
    
                //start loading
                queue.load();
    
    
    
            }
    
            protected function childProgressHandler(event:LoaderEvent):void
            {
                var procent:Number = Math.floor(event.target.progress*100);
                var targetName:String = event.target.name;
                trace(procent+'% loaded of item: '+targetName);
            }
    
            protected function childCompleteHandler(event:LoaderEvent):void
            {
                var targetName:String = event.target.name;
                trace(targetName+' is loaded!');
            }
    
    
            private function completeHandler(event:LoaderEvent):void {
    
                var objects:Array = event.currentTarget.content;
    
                for(var i:uint=0; i < objects.length; i++)
                {
    
                    var image:ContentDisplay = LoaderMax.getContent(objects[i].name);
                    TweenLite.to(image, 1, {alpha:1, y:100});
    
                }
                trace(event.target + " is complete!");
            }
    
            private function errorHandler(event:LoaderEvent):void {
                trace("error occured with " + event.target + ": " + event.text);
            }
        }
    }