I am a blogger and Flash Develper.You can ask me to write tutorials on any topic you want.

Making a simple text preloader in Flash

| Saturday, June 20, 2009


Flash Source File
Download this flash document preloader.fla



Whenever we make any web content like website or a browser game using Flash, the size of the it may typically range from few hundred KiloBytes(KB) to few MegaBytes(MB).The swf contains a large number of media objects like movie clips,buttons,graphics,sound,videos.When the user is waiting for the page to load,there might be chances that the server is slow or the user connection is slow or the swf might not be loaded completely.In any of these cases the user will see a partially loaded swf which might not play or he might see a blank swf.Confused,he might leave the page.

To avoid this we can include a preloader in Flash,that shows how much of data is loaded and this also does not allow the swf to play until its completely loaded.

In the very first frame,add a text box with the text tool and set its properties to 'dynamic text'.







In the instance name below type its name as number_txt.







Add a layer at the top and name it actionScript.












Place the following code in the frame 1 of actionscript layer.





var current:Number;

var total:Number;
var loaded:Number;
var duration:Number=100;
var loadInterval:Number;

total = _root.getBytesTotal();
loadInterval = setInterval(this,"preloader",duration);

function preloader() {
current = _root.getBytesLoaded();
loaded = Math.round(current/total*100);
_root.number_txt.text=loaded;
if(current>=total){
gotoAndPlay(2);
clearInterval(loadInterval);
}
}

stop();

KeyFrames
_root.getBytesLoaded() returns the number of bytes that are currently loaded into the user's PC.

_root.getBytesTotal() returns the size of the swf.

current/total*100 gives the percentage of the file loaded.

Math.round(value:Number) rounds the value to an integer and returns the value.

Make sure the interval is cleared at the end.

Put some media in the later frames and test your document.

Related Posts
Simulate Download:Bandwidth Profiler