How to create a preloader in AS3

My flash applications are a bit big, so I want to embed the preloader in my application, So who can tell me how to create a preloader in the new โ€œSceneโ€ and load another scene after the preload is completed?

Thanks at Advance!

+6
source share
5 answers

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); //additional code to update preloader graphics //.. if (percent == 100) onLoaded(); } function onLoaded():void { loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress); var application:DisplayObject = loader.content; addChild(application); } 

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() { //add preloader graphics //check loading progress this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress); } private function onProgress(e:ProgressEvent):void { var percent:Number = Math.round(e.bytesLoaded / e.bytesTotal * 100); if (percent == 100) { this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress); onLoaded(); } } private function onLoaded():void { nextFrame(); //go to next frame var App:Class = getDefinitionByName("Main") as Class; //class of your app addChild(new App() as DisplayObject); } } } 
+12
source

I recommend switching to the โ€œeasy swf loads heavy swfโ€ route, since it is the most efficient that I have seen. There are a lot of bloated tutorials out there, but for me I like it http://doogog.com/actionscript-3-external-preloader.html It's straight to the point.

+1
source

Here's an article on how to create an as3 built-in preloader http://www.kirupa.com/forum/showthread.php?351689-actionscript-preloader-in-flash-develop

0
source

You can also use a third-party library such as Greensock to preload both before launching the application and when downloading application files.

I personally use Greensock and recommend it. It resolves some errors with the loaderInfo class.

http://www.greensock.com/loadermax/

0
source

[Frame (factoryclass = 'Preloader')] no longer works in the new ASC 2.0 compiler. Instead, the preloader becomes your document class, and you need to add an additional Compiler argument to tell flash to include your main class in the second frame (do not refer to it in the preloader):

-reper = NameDoesntMatter, Main

Does ASC 2.0 recognize Frame metadata tags [example: for Preloader factoryClass)?

ASC2.0 and frame meta tag

0
source

Source: https://habr.com/ru/post/903251/


All Articles