开发者

How to use loaderInfo?

开发者 https://www.devze.com 2023-03-21 19:07 出处:网络
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?

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 );
     }


you could just use the PROGRESS event:

mapLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
// ...
private function progressHandler(event:ProgressEvent):void
{
  trace("Downloaded " + event.bytesLoaded + " out of " + event.bytesTotal + " bytes");
}


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.


stop();
var mapLoader : Loader = new Loader( );
function engine():void {    

    mapLoader.load( new URLRequest( "index.swf" ) );
    mapLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler );
    mapLoader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, onProgressAction);    
}
function onProgressAction (e:ProgressEvent):void 
{
    var loaded:Number = e.target.bytesLoaded;
    var total:Number = e.target.bytesTotal;
    var percent:Number(loaded/total) * 100;
}    
function completeHandler( eventOBJ : Event ):void    {
    stage.addChild( eventOBJ.target.content );
    mapLoader.contentLoaderInfo.removeEventListener( ProgressEvent.PROGRESS, onProgressAction);  
}
engine();

This will works

0

精彩评论

暂无评论...
验证码 换一张
取 消