A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Dynamic image preloaders problem

Hybrid View

  1. #1
    Junior Member
    Join Date
    Sep 2007
    Posts
    8

    Dynamic image preloaders problem

    Alright. I've searched the forum and google but I haven't gotten an answer to my problem.

    I'll break it down for you.

    I have a gallery, loading images via xml and then using a for loop to add them inside a movieclip.

    Now, I need a preloader which shows the progress for each images individually. I haven't figured out how to do this since my urlloader only shows the progress of all images (I think).

    Here's what my xml function looks like:

    PHP Code:
    public function renderXML(event:Event):void
            
    {
                var 
    gallery:XML = new XML(event.target.data);
                
    numberPictures gallery.picture.length();
                for (
    i=0<gallery.picture.length(); i++)
                {
                
                    
    xmlObject = new MovieClip();
                    
    xmlObject.= (Step) + 20;
                    
    xmlObject.30;
                    
    galleryRender.galleryItems.addChild(xmlObject);
                
                    var 
    thumbLoader:Loader = new Loader();
                    
    xmlObject.addChild (thumbLoader);
                    
    thumbLoader.load (new URLRequest(gallery.picture[i].imagepath));
                    
    thumbLoader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESSthumbLoadProgress);
                    
                    
    loaderText = new TextField();
                    
    xmlObject.addChild(loaderText);            
                }
                
                function 
    thumbLoadProgress (event:ProgressEvent):void
                    
    {
                        var 
    pcent:Number=Math.ceil((event.currentTarget.bytesLoaded event.currentTarget.bytesTotal) * 100);
                        if (
    pcent != 100)
                        {
                            
    event.currentTarget.loaderText.text = ("Loading images: " pcent "%");
                            
    event.currentTarget.loaderText.visible true;
                        }
                        else
                        {
                            
    event.currentTarget.loaderText.visible false;
                        }
                    }
            } 
    Thanks in advance!

  2. #2
    Senior Member
    Join Date
    Sep 2005
    Location
    Detroit
    Posts
    193
    URLLoader will work in this situation. I see a problem with your code, which is why it isn't working. In your for loop you declare thumbLoader as a new variable. Problem is you are declaring that variable as many times as the for loop goes around. Loader is asyncronous so you can be loading all of your images at the same time and therefore your listener is responding too all of the events of all of your images (or just one if the code executed as expected).

    Easy fix:
    [CODE]
    var length:int = gallery.pictures.length;
    var i:int = 0;
    function getPicture():void{
    xmlObject = new MovieClip();
    xmlObject.x = (i * Step) + 20;
    xmlObject.y = 30;
    galleryRender.galleryItems.addChild(xmlObject);

    var thumbLoader:Loader = new Loader();
    xmlObject.addChild (thumbLoader);
    thumbLoader.contentLoaderInfo.addEventListener(COM PLETE, Loaded);

    function Loaded(event:Event):void{

    thumbLoader.load (new URLRequest(gallery.picture[i].imagepath));
    thumbLoader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS, thumbLoadProgress);

    loaderText = new TextField();
    xmlObject.addChild(loaderText);
    While the music played you worked by candlelight
    Those san francisco nights
    You were the best in town
    Just by chance you crossed the diamond with the pearl
    You turned it on the world
    That’s when you turned the world around

  3. #3
    Senior Member
    Join Date
    Sep 2005
    Location
    Detroit
    Posts
    193
    URLLoader will work in this situation. I see a problem with your code, which is why it isn't working. In your for loop you declare thumbLoader as a new variable. Problem is you are declaring that variable as many times as the for loop goes around. Loader is asyncronous so you can be loading all of your images at the same time and therefore your listener is responding too all of the events of all of your images (or just one if the code executed as expected).

    Easy fix:
    Code:
    var length:int = gallery.pictures.length;
    var i:int = 0;
    function getPicture():void{
       xmlObject = new MovieClip();
       xmlObject.x = (i * Step) + 20;
       xmlObject.y = 30;
       galleryRender.galleryItems.addChild(xmlObject);
                
       var thumbLoader:Loader = new Loader();
       xmlObject.addChild (thumbLoader);
       thumbLoader.contentLoaderInfo.addEventListener(COMPLETE, Loaded);
       
       function Loaded(event:Event):void{
          i++;
          getPicture();
       }
    
       thumbLoader.load (new URLRequest(gallery.picture[i].imagepath));
       thumbLoader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS, thumbLoadProgress);
                    
       loaderText = new TextField();
       xmlObject.addChild(loaderText);
    }
    untested.
    of course you can clean this up even more; I just typed this in a few minutes. This way you wait until picture[0] is loaded before picture[1].
    While the music played you worked by candlelight
    Those san francisco nights
    You were the best in town
    Just by chance you crossed the diamond with the pearl
    You turned it on the world
    That’s when you turned the world around

  4. #4
    Senior Member
    Join Date
    Sep 2005
    Location
    Detroit
    Posts
    193
    ignore that first reply. I accidentily hit tab space. also this line

    Code:
    thumbLoader.contentLoaderInfo.addEventListener(COMPLETE, Loaded);
    should be
    Code:
    thumbLoader.addEventListener(COMPLETE, Loaded);
    While the music played you worked by candlelight
    Those san francisco nights
    You were the best in town
    Just by chance you crossed the diamond with the pearl
    You turned it on the world
    That’s when you turned the world around

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center