Search code examples
flashactionscript-3programming-languagesloading

How to use loaderInfo?


I want to make a loading bar but at first I need to have loading info per enter frame. This is how I do it but it seem not working. Could you teach me how to do it?

 var mapLoader : Loader = new Loader( );
     var mapLoaderInfoLoad:Number;
     var mapLoaderInfoTotal:Number;

public function engine() 
{

        addEventListener( Event.ENTER_FRAME, onEnterFrame,false,0,true );

        mapLoader.load( new URLRequest( "Mapcontrol.swf" ) );
        mapLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler );

}
private function onEnterFrame( evt:Event ):void
    {mapLoaderInfoLoad = mapLoader.loaderInfo.bytesLoaded;
        mapLoaderInfoTotal = mapLoader.loaderInfo.bytesTotal;
        trace(mapLoaderInfoLoad);
        trace(mapLoaderInfoTotal);}

public function completeHandler ( eventOBJ : Event ) : void
     {
         stage.addChild( mapLoader.content );
     }

Solution

  • The bytesTotal of a Loader's loaderInfo will return 0 until the Loader has fired it's first progress event. Is there a reason why you want to use an enterFrame in this way rather than the progress event?

    Simple Progress Event Example:

    var mapLoader : Loader = new Loader();
    var mapLoaderInfoLoad:Number;
    var mapLoaderInfoTotal:Number;
    
    public function engine() 
    {
        mapLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler );
        mapLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        mapLoader.load( new URLRequest( "Mapcontrol.swf" ) );  
    }
    
    private function progressHandler(evt:ProgressEvent):void
    {
        mapLoaderInfoLoad = evt.bytesLoaded;
        mapLoaderInfoTotal = evt.bytesTotal;
        trace(mapLoaderInfoLoad);
        trace(mapLoaderInfoTotal);
    }
    

    Enter Frame

    Of course, there might be a reason for you to use an enterFrame event. You could still do so as in your example, but let the progressHandler from my example populate your variables; or wait until the first progress event is fired, then delete the progress event listener and add your enterFrame listener instead. As long as you don't try to read mapLoader.loaderInfo.bytesTotal before the first progress event is fired you should be OK.