dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: How to check if Image is preloaded

  1. #1
    xxmagic77
    Guest

    How to check if Image is preloaded

    hi to all,

    ive written a small class for a image galery with preloader. but how to check if a image allready is loaded to hide preloader bar?

    i tried to discover with httpStatus, but unfortunetly this event is dispatched after the open/progress event - maybe someone can give me an hint

    the code comes here

    main class:
    Code:
    package marek {
        import flash.display.*;
        import flash.net.*
        import flash.events.*;
        import flash.utils.*;
        import flash.filters.BlurFilter;
    
        import com.greensock.*; 
        import com.greensock.easing.*;
    
      public class main extends Sprite {
            private var xmlCnt:XML;
            private var imgLst:XMLList;
            private var urlLdr:URLLoader;
            private var bmp:Bitmap;
            private var imgHolder:MovieClip;
            private var loader:Loader;
            private var mainTimer:Timer;
            private var imgBlurFilter:BlurFilter;
            private var loaderbar:LoaderBar;
            private var imgTween:TweenMax;
            
            public function main() {
                stage.scaleMode = StageScaleMode.NO_SCALE;
                stage.align = StageAlign.TOP_LEFT;
                XMLLoader();
            }
    
            private function XMLLoader():void {
                XML.ignoreComments = true;
                XML.ignoreWhitespace = true;
                urlLdr = new URLLoader();
                urlLdr.addEventListener(Event.COMPLETE, completeListenerXML);
                urlLdr.load(new URLRequest("main.xml"));
            }
    
            private function completeListenerXML(e:Event):void {
                xmlCnt = new XML(urlLdr.data);
                imgLst = xmlCnt.galery.item;
                init();
            }
    
            private function init():void {
                imgHolder = new MovieClip();
                addChild(imgHolder);
                bmp = new Bitmap();
                //            
                mainTimer = new Timer(xmlCnt.galery.@delay);
                mainTimer.addEventListener(TimerEvent.TIMER, mainTimerEvent);
                mainTimer.start();
            }
    
            private function mainTimerEvent(e:TimerEvent):void {
                // interrupt timer
                mainTimer.stop();
                addImage(mainTimer.currentCount % imgLst.length());
            }
    
            private function addImage(i):void {
                loader = new Loader();
                //
                loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
                loader.contentLoaderInfo.addEventListener(Event.OPEN, openHandler);
                loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
                //
                loader.load(new URLRequest(imgLst[i].@image));
                addChild(loader);
          }
    
            private function httpStatusHandler(e:HTTPStatusEvent):void {
                trace("httpStatusHandler")
                //trace("httpStatusHandler: " + e);
            //trace("status: " + e.status);
                
                switch (e.status) {
                        case 200:
                                // Valid image, do nothing.
                                break;
                        case 404:
                                // Invalid image
                                break;
                        default:
                                break;
                }
            }
    
            private function openHandler(e:Event):void {
                trace("openHandler")         
                loaderbar = new LoaderBar(e);
                addChild(loaderbar);
            }
    
            private function progressHandler(e:Event):void {
                trace("progressHandler")
                //trace("progressHandler event: ")
                loaderbar.updateLoader();
            }
            
            private function initHandler(e:Event):void {
                trace("initHandler")
            }
    
            private function completeHandler(e:Event):void {
                trace("completeHandler")
                e.target.removeEventListener(Event.OPEN, openHandler);
                e.target.removeEventListener(Event.COMPLETE, completeHandler);
                e.target.removeEventListener(ProgressEvent.PROGRESS, progressHandler);            
                //
                TweenMax.to(loaderbar, 1, {alpha:0, ease:Sine.easeInOut, onComplete:completeListenerLoaderBar});
                //
                //trace("before: " + imgHolder.numChildren)
                while (imgHolder.numChildren > 1) {
                    imgHolder.removeChildAt(0);
                }
                //trace("after: " + imgHolder.numChildren)
                //
                bmp = e.currentTarget.content;
                bmp.smoothing = true;
                bmp.x = bmp.y = 0;
                bmp.alpha = 0;
                imgHolder.addChild(bmp);
                //
                imgBlurFilter = new BlurFilter(xmlCnt.galery.@blurX, xmlCnt.galery.@blurY, 2);
            bmp.filters = [imgBlurFilter];
                //
                imgTween = new TweenMax(bmp, 4, {alpha:1, ease:Sine.easeInOut, onComplete:completeListenerTween,blurFilter:{blurX:0, blurY:0, ease:Sine.easeInOut}});
                //
            }
    
            
            // tweenmax listener
            private function completeListenerLoaderBar():void {
                removeChild(loader);
                removeChild(loaderbar);
            }
            
            private function completeListenerTween():void {
                // resume mainTimer
                mainTimer.start();
            }
            
        }
    }
    LoaderBar class
    Code:
    package marek {
        import flash.display.*;
        import flash.events.*;
        
        public class LoaderBar extends Sprite {
            private var eObj:Event;
            private var sBar:Sprite;
            
            public function LoaderBar(e:Event) {
                this.eObj = e;
          this.sBar = new Sprite;
                this.sBar.graphics.moveTo(0, 0);
                this.sBar.graphics.lineStyle(3, 0xff6600);
                addChild(sBar);
            }
            
            public function updateLoader() {
                this.sBar.graphics.lineTo((this.eObj.currentTarget.bytesLoaded / this.eObj.currentTarget.bytesTotal * 100), 0);
            }
            
        }
    }

  2. #2
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    [php]

    i'd put int the complete event or someplace(you will have to figure it out);
    PHP Code:
     if ( ==xml children .length ){
    //remove progress bar 

    ~calmchess~

  3. #3
    xxmagic77
    Guest
    So ive rewritten
    the code a little bit and played with the getTimer function to
    hide/show loaderbar. Maybe its not the best solution but anyway it seems to work - i saw that theres a SOCKET_DATA Event that could be used, but this seem to be overloaded for such a small api like this.

    so heres the code

    main class:
    Code:
    package marek {
        import flash.display.*;
        import flash.net.*
        import flash.events.*;
        import flash.utils.*;
        import flash.filters.BlurFilter;
    
        import com.greensock.*; 
        import com.greensock.easing.*;
    
      public class main extends Sprite {
            private var xmlCnt:XML;
            private var imgLst:XMLList;
            private var urlLdr:URLLoader;
            private var imgData:BitmapData;        
            private var bmp:Bitmap;
            private var imgHolder:MovieClip;
            private var loader:Loader;
            private var mainTimer:Timer;
            private var imgFilter:BlurFilter;
            
            private var i:uint;
            private var startT:uint;
            private var endT:uint;
            
            private var loaderbar:LoaderBar;
            private var imgTween:TweenMax;
            
            public function main() {
                stage.scaleMode = StageScaleMode.NO_SCALE;
                stage.align = StageAlign.TOP_LEFT;
                XMLLoader();
            }
    
            private function XMLLoader():void {
                XML.ignoreComments = true;
                XML.ignoreWhitespace = true;
                urlLdr = new URLLoader();
                urlLdr.addEventListener(Event.COMPLETE, completeListenerXML);
                urlLdr.load(new URLRequest("main.xml"));
            }
    
            private function completeListenerXML(e:Event):void {
                xmlCnt = new XML(urlLdr.data);
                imgLst = xmlCnt.galery.item;
                init();
            }
    
            private function init():void {
                imgHolder = new MovieClip();
                addChild(imgHolder);
                //
                mainTimer = new Timer(500);
                mainTimer.addEventListener(TimerEvent.TIMER, mainTimerEvent);
                mainTimer.start();
            }
    
            private function mainTimerEvent(e:TimerEvent):void {
                // pause timer
                mainTimer.stop();
                addImage(mainTimer.currentCount % imgLst.length());
            }
    
            private function addImage(i:uint):void {
                loader = new Loader();
                loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
                loader.contentLoaderInfo.addEventListener(Event.OPEN, openHandler);
                loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
                loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
                loader.load(new URLRequest(imgLst[i].@image));
          }
    
            private function httpStatusHandler(e:HTTPStatusEvent):void {
                trace("4. httpStatusHandler")
                
                //trace("httpStatusHandler: " + e);
               //trace("status: " + e.status);
                
                switch (e.status) {
                        case 200:
                                // Valid image, do nothing.
                                break;
                        case 404:
                                // Invalid image
                                break;
                        default:
                                break;
                }
                
            }
    
            private function openHandler(e:Event):void {
                trace("1. openHandler")
                startT = getTimer();
                loaderbar = new LoaderBar(e);
                addChild(loaderbar);
            }
    
            private function progressHandler(e:Event):void {
                trace("2. progressHandler")
                endT = getTimer();
                if ((endT - startT) >= 500) {
                    loaderbar.updateLoader();
                } else {
                    loaderbar.reset();
                }
            }
    
            private function initHandler(e:Event):void {
                trace("3. initHandler")
                imgData = Bitmap(loader.content).bitmapData;
                bmp = new Bitmap(imgData);
                imgFilter = new BlurFilter(xmlCnt.galery.@blurX, xmlCnt.galery.@blurY, 2);
            bmp.filters = [imgFilter];
                bmp.alpha = 0;
            }
    
            private function completeHandler(e:Event):void {
                trace("5. completeHandler")
                e.target.removeEventListener(Event.OPEN, openHandler);
                e.target.removeEventListener(ProgressEvent.PROGRESS, progressHandler);    
                e.target.removeEventListener(Event.INIT, initHandler);
                e.target.removeEventListener(Event.COMPLETE, completeHandler);
                //
                TweenMax.to(loaderbar, 1, {alpha:0, ease:Sine.easeInOut, onComplete:completeListenerLoaderBar});
                //trace("before: " + imgHolder.numChildren)
                while (imgHolder.numChildren > 1) {
                    imgHolder.removeChildAt(0);
                }
                //trace("after: " + imgHolder.numChildren)            
                imgHolder.addChild(bmp);
                imgTween = new TweenMax(bmp, 4, {alpha:1, ease:Sine.easeInOut, onComplete:completeListenerTween,blurFilter:{blurX:0, blurY:0, ease:Sine.easeInOut}});
                //
            }
    
            // tweenmax listeners
            private function completeListenerLoaderBar():void {
                removeChild(loaderbar);
            }
            
            private function completeListenerTween():void {
                // resume mainTimer with new delay
                mainTimer.delay = uint(xmlCnt.galery.@delay);
                mainTimer.start();
            }
    
        }
    }
    LoaderBar class:
    Code:
    package marek {
        import flash.display.*;
        import flash.events.*;
        
        public class LoaderBar extends Sprite {
            private var eObj:Event;
            private var sBar:Sprite;
            
            public function LoaderBar(e:Event) {
                this.eObj = e;
          this.sBar = new Sprite;
                this.sBar.graphics.moveTo(0, 0);
                this.sBar.graphics.lineStyle(3, 0xff6600);
                addChild(sBar);
            }
            
            public function updateLoader() {
                this.sBar.graphics.lineTo((this.eObj.currentTarget.bytesLoaded / this.eObj.currentTarget.bytesTotal * 100), 0);
            }
            
            public function reset() {
                this.sBar.graphics.clear();
            }
        }
    }

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