Creating a main preloader in Flash CS3
Posted on February 15, 02008
Filed Under DaVinci, Development, Flash
One of the first issues you'll encounter when migrating from AS2 is that you cannot place any AS3 code on the timeline. All AS3 code lives in external class files. The next thing that will cross your mind is this:
"If you can't place code on the timeline and you can't use an AS3 class until it's loaded in, how the hell do you write a preloader?"
If you work with Flex then this isn't your problem - the compiler will take care of preloading for you. If you work with Flash CS3 you'll need to get used to a few new ways of working.
The Document class
FLA files for an AS3 project can have a root document class. This is the equivalent of using Linkage to associate a class with a library item, but in this case you are linking a class to the Stage. This class is immediately available and in most cases you will use it to preload the rest of the SWF.
Garbage collection
One thing missing from most preloader solutions I've found is garbage collection. This is a really big deal in AS3. Grant Skinner covered it much better than I ever could in a series of posts about AS3 Resource Management. One of the best ways to remember to do this is to always write a destroy() function alongside your constructor. This function should remove any listeners you may have set up during the life of the class.
MainPreloader.as
The code below contains only the bare essentials required to get your preloader working. In the next post I'll show you an easy way to customise the look of your preloader.
package
{
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.display.MovieClip;
import flash.events.*;
public class MainPreloader extends MovieClip
{
public function MainPreloader():void
{
super();
init();
}
protected function init():void
{
stop();
this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onMainLoadProgress);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
}
protected function destroy():void
{
this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onMainLoadProgress);
}
protected function onMainLoadProgress(e:ProgressEvent):void
{
var percent:Number = Math.round((e.bytesLoaded / e.bytesTotal )*100 );
preloader.gotoAndStop( percent );
trace(percent + "% loaded");
if (e.bytesLoaded == e.bytesTotal ){ onMainLoaded() };
}
protected function onMainLoaded():void
{
destroy();
play();
}
}
}
Related Journal Entries
- Creating an animated main preloader in Flash CS3
- Big History
- The project architecture
- Hacking the Date class for GeoLocation
- AS3 Component: TimeStepper
- AS3 Component: NumberStepper
- AS3 component: NextPreviousButton
- Component framework July update
- Building an AS3 component framework: Suspend and Resume
- Building an AS3 component framework: Events and Inheritance
- Building an AS3 component framework: Deferred Rendering
- Inside the Bit-101 MinimalComps
- Building an AS3 component framework: Planning
- Choosing an AS3 component framework
- Projects update March 2009
Comments
Leave a Comment
If you would like to make a comment, please fill out the form below.


i actually have a ?
in as2 i used a preloader within the movie
itself ,no call out to as or another movie very simple .heres the code
frame1 ifFrameLoaded (“middle”) {
gotoAndPlay (“start”);
}
frame2 gotoAndPlay (1);
frame 3 lable= start
in the middle of the movie say frame 140 i labled
it middle.
while the movie loaded up to “middle” a simple animation in frame 1 played.
please help me to impliment this into as3.
why is asn so simple to do this and as3 is all this mindboggling code its rediculous unless your loading an outside source.
i keep getting errors with this code in as3.
your code like others works fine, but i dont want to load the entire movie , most of my movies are 4+ megs and i dont want someone to wait forever ,that why i use flash , it supposed to stream to people.
I hope you can help me.sorry to be a bother
help would be most appreciated.
Hiya Stormbringer, yeah AS3 has a bit more code than the AS2 timeline version but the main reason for keeping everything in its own AS3 class file is so you can re-use it from project to project. The point is to write it once and never have to think about it again.
What do you mean by “you cannot place any AS3 code on the timeline”? You most certainly can put AS3 code in the timeline.
Although sometimes there are unexpected results if you have timeline code and a document class in the same file.
Hiya Subtropical, you’re right of course – timeline code does work. I think the point I was trying to make was that moving from AS2 to AS3 you should get out of the habit of using timeline code and start using classes. The dilemma then becomes: how do you use a class for your preloader if you are preloading said classes? The solution is the document class. Sorry for the confusion – my bad.