Myforce22
Myforce22

Reputation: 21

Flash Preloader as3

Is it possible to set total bytes in an as3 preloader? I'm not sure it's correct, but to avoid an infinite loop I've done this.

var loadper:Number=0;
var total_bytes:Number = 3484484;
loaderInfo.addEventListener(ProgressEvent.PROGRESS, loader);

function loader(filename:ProgressEvent) { 
   var loaded_bytes:Number = stage.loaderInfo.bytesLoaded;
   _txt.text=String(loadper+"%");
   if(loadper>=100) { 
      preLoader_mc.perct_mc.visible=false;
      loaderInfo.removeEventListener(ProgressEvent.PROGRESS, loader);
   }
}

Upvotes: 1

Views: 745

Answers (2)

konsnos
konsnos

Reputation: 34

In the same event there is the information about total bytes.

You can have it as

filename.bytesTotal

where filename is your event variable as per your code.

By doing

filename.bytesLoaded / filename.bytesTotal

you have the percent completion. This would never enter an infinite loop.

You can check more detailed information on adobe's documentation and examples here.

Upvotes: 0

xLite
xLite

Reputation: 1481

Just listen out for Event.COMPLETE which triggers once the file has fully loaded. Along with a few other tweaks:

loaderInfo.addEventListener(ProgressEvent.PROGRESS, loaderProgress);
loaderInfo.addEventListener(Event.COMPLETE, loaderComplete);

function loaderProgress(event:ProgressEvent) { 
    var percentage:Number = Math.round((stage.loaderInfo.bytesLoaded / stage.loaderInfo.bytesTotal) * 100);
    _txt.text = percentage + "%";
}

function loaderComplete(event:Event):void
{
    loaderInfo.removeEventListener(ProgressEvent.PROGRESS, loaderProgress);
    loaderInfo.removeEventListener(Event.COMPLETE, loaderComplete);

    preLoader_mc.perct_mc.visible = false;
}

Upvotes: 1

Related Questions