2

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?

Community
  • 1
  • 1
Yovo
  • 629
  • 3
  • 8
  • 16
  • Why would loading images synchronously improve the performance? Also the PreLoader you linked to is asynchronous. What it does is load the images *asynchronously* one at a time, and not as you do at the same time. – sch Feb 19 '12 at 14:56
  • @sch Yes what I want is really load them one by one or max 3-4 images at once. The problem is when the browser is loading 10 or more images at once then it frees for a while. When I use this PreLoader class browser is OK but as I pointed you cant add mouse event listener to loader. – Yovo Feb 19 '12 at 15:07

2 Answers2

1

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);
        }
    }
}
Nicholas
  • 5,430
  • 3
  • 21
  • 21
0

Check this out: http://www.greensock.com/loadermax/

Let's you specify max number of simultaneous loaders

arieljake
  • 515
  • 5
  • 11