Update:
Option 1. Flash IDE, one SWF file
To have a built-in preloader when compiling with the Flash IDE, you must transfer your Document Class code to the second frame of your FLA file (of course, without the package and class constructor) and remove the Document Class .as file from the project property. In the first frame you should put this code:
stop(); // stops the timeline at preloader frame this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress); function onProgress(e:ProgressEvent):void { var percent:Number = Math.round(e.bytesLoaded / e.bytesTotal * 100); //additional code to update preloader graphics //.. if (percent == 100) onLoaded(); } function onLoaded() { this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress); nextFrame(); }
After loading swf, it proceeds to the next frame and the application initialization source code must be executed. This works well if you organize your project so that most of the assets (images, etc.) are in the Flash IDE library and are not loaded in the first frame (you can check this in the properties of each library element).
Option 2. Flash IDE, two swf files
Another option, as another commentator has already recommended, is to keep your swf application as it is, and create another lightweight swf that will load the first one. The preloader.swf code in the first frame:
var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress); loader.load(new URLRequest("path/to/application.swf")); function onProgress(e:ProgressEvent):void { var percent:Number = Math.round(e.bytesLoaded / e.bytesTotal * 100);
Sometimes there are additional problems with this approach when you try to access the stage from your Document Class constructor , etc., but in most cases this should do the job.
Option 3. Various IDEs, my recommendation: FlashDevelop
If you tried to compile my originally published code using FlashDevelop, Flash Builder, or any other development environment, it should work.
Original post:
Here's the basic setup of the built-in preloader. Your Document Class should look like this:
package { import flash.display.Sprite; [Frame(factoryClass='Preloader')] //class name of your preloader public class Main extends Sprite { public function Main() { //init } } }
Preloader Class:
package { import flash.display.DisplayObject; import flash.display.MovieClip; import flash.events.ProgressEvent; import flash.utils.getDefinitionByName; public class Preloader extends MovieClip { public function Preloader() {