Preloader




Here i will show you how to make a preloader with flash (AS2 and AS3)

Navigation


Getting started

Making a preloader is very simple. At first, create 3 frames on the maintime line.  The first frame is the preloader, the second one a buffer and the last one the game.  Now you need to change the export settings of all classes like movie clips and sounds.  They are exported on frame 1, which means its loaded before the movie is loaded.  You need to uncheck "Export on frame 1". Then drag and drop them all on frame 2.  For sounds you need to create a new layer.


AS2

Making a preloader in AS2 is easier than in AS3. Here is an example:
function onEnterFrame(){
    if(_root.getBytesLoaded() == _root.getBytesTotal()){
        delete onEnterFrame
        //Other code
    }
}
This a very simple example for an AS2 preloader. You can add a loading bar or other stuff.


AS3

AS3 is a little bit harder than AS2. There is a "good" and a "bad" preloader. 
Internet explorer is working different than other browsers, so it can cause you can play the game only once. 

Bad code:
package{
    import flash.events.Event
    import flash.events.ProgressEvent
    import flash.display.MovieClip
    public class main extends MovieClip{
        public function main():void{
            this.stop()
            this.loaderInfo.addEventListener(ProgressEvent.PROGRESS,loading)
            this.loaderInfo.addEventListener(Event.COMPLETE,done)
        }
        private function loading(e:ProgressEvent):void{
            this.preloader.text = String(e.bytesLoaded / e.bytesTotal * 100) + "%"
        }
        private function done(e:Event):void{
            this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS,loading)
            this.loaderInfo.removeEventListener(Event.COMPLETE,done)
            this.gotoAndStop(3)
        }
    }
}
Internet explorer loads files from the Temporary Internet Files. Its possible the "COMPLETE" is dispatched before the eventlistener was added.
You can add now a timer or use the other way:
package{
    import flash.events.Event
    import flash.display.MovieClip
    public class main extends MovieClip{
        public function main():void{
            this.stop()
           this.addEventListener(Event.ENTER_FRAME,loading)
        }
        private function loading(e:Event):void{
           this.preloader.text = String(this.loaderInfo.bytesLoaded / this.loaderInfo.bytesTotal * 100) + "%"
           if(this.loaderInfo.bytesLoaded == this.loaderInfo.bytesTotal){
              this.gotoAndStop(3)
              this.removeEventListener(Event.ENTER_FRAME,loading)
           }
        }
    }
}
So, now you can start making your own preloader.