Categories
Featured jobs
» More ActionScript, Flash and Flex jobs.
» Advertise a job for free
Our network
Advertisement

 »  Home  »  Tutorials  »  Flash  »  Beginner  »  Adding a PreLoader to your AS3 project

Adding a PreLoader to your AS3 project

By Paul Schoneveld | Published 04/8/2008 | Beginner | Rating:
Paul Schoneveld
I'm a hard core ActionScript 3.0 programmer.  I wouldn't go back to working in AS 2 if you paid me (well, how much are you offering?).
I work for ClickPopMedia as their lead ActionScript/Flash developer.
 

View all articles by Paul Schoneveld
Creating your preloader

In ActionScript 2 the _root (or Stage) had the methods getBytesLoaded() and getBytesTotal(). In AS3, we no longer have _root, or either of the getBytes methods (on the stage at least). So, how are we going to check and display the load progress?
It's simple enough. The methods have really just been internalized and made private. We are now provided with 2 read-only properties in the stage.loaderInfo called bytesLoaded and bytesTotal.

So, to get started with your preloader, you will need some code similar to this:

public function DocumentClass() {
    //I usually throw down a stop(); on the main timeline frame 1.
    addEventListener(Event.ENTER_FRAME, handleProgress);
}
private function handleProgress(event:Event):void {
    var loaded:Number = stage.loaderInfo.bytesLoaded
    var total:Number = stage.loaderInfo.bytesTotal
    var percent:Number = loaded/total;
//    trace(Math.floor(percent*100)+"%");
    UpdateProgress(percent);
    if(loaded >= total){
        removeEventListener(Event.ENTER_FRAME, handleProgress);
        gotoAndPlay('begin');
    }
}


We avoid using the PROGRESS and COMPLETE events by using the ENTER_FRAME event. This is a matter a preference I suppose, but I have heard that PROGRESS and COMPLETE events don't work 100% of the time for the stage. Not sure why, but I find things work just fine without them.

So that bit of code is going to run a method called UpdateProgress() every frame until the movie is fully loaded. What you do in the method UpdateProgress is up to you. You could simply display the progress in a TextField:

private function UpdateProgress(prog:Number):void
{
    output_TF.text = String(Math.floor(prog * 100)+"%");
}


or you could have it slowly a mask across a load bar:

private function UpdateProgress(prog:Number):void {
   loadProg_MC.x = initX - (initX * prog);
}

OR you could make an animated movie clip (say, a sunrise) and pregress through the frames at the speed of the load progress:

private function UpdateProgress(prog:Number):void {
    var frame:Number = Math.floor(prog*200); //200 frames makes a smoother animation for preloading.  Otherwise it's jumpy.
    myPreloader.gotoAndStop(frame);
}

Or any combination or cool new idea you can come up with. That is really a project specific thing that I cannot anticipate fully for you.

Once you've built your preloader you can test it by adding some music to the stage on frame 2 or something. You might find that your preloader isn't showing up until everything is finished loading... that really defeats the purpose of a preloader don't you think?

On the next page I'm going to talk about how to avoid the common pitfalls that will stop your perfectly good preloader from doing it's job.



Spread The Word / Bookmark this content

Clesto Digg it! Reddit Furl del.icio.us Spurl Yahoo!

Related Articles
Comments



Search Entire Site
Add to Google
Advertisements
Article Options
Latest New Articles
Set up a simple IIS Server for Flash
by Peter McBride

Day 1 at FITC Toronto 2008
by Anthony Pace

Simple reflection effect with AS2
by Jean André Mas

ActionScript.org Meets Josh Tynjala (aka dr_zeus)
by ActionScript.org Staff

Rapidly Create Online Flash Movies to Help Users Market, Sell and Support Software and Hardware
by Sabrina F

mailing list
Enter your email address:
mailing list
Subscribe Unsubscribe
© 2000-2007 actionscript.org! All Rights Reserved.
Read our Privacy Statement and Terms of Use...
Our dedicated server is hosted and managed by WebScorpion Webhosting.