Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

flash - How to create Preloader in AS3

My flash applications is little bit big, so i want to embed a preloader in my application, So can anyone please tell me how to create a preloader in new 'Scene' and load another scene in after preloading completed?

Thanks in Advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update:

Option 1. Flash IDE, one swf file

To have an embedded preloader when compiling with Flash IDE, you should move your Document Class code to 2nd frame of your FLA file (without package and class constructor, of course), and remove Document Class .as file from project properties. In the first frame you should place such 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();
}

Once swf is loaded, it advances to the next frame and the original application initialization code should be executed. This works well if you organized your project in a way that the most of the assets (images, etc) are in the Flash IDE Library and are not loaded on the first frame (you can check that in each library item's properties).

Option 2. Flash IDE, two swf files

Another option, as already recommended by another commenter, is to keep your application swf as it is, and create another lightweight swf that would load the first one. The code of preloader.swf in 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 issues with this approach, when you try to access stage from your Document Class constructor etc, but for most cases this should do the job.

Option 3. Different IDE, my recommendation: FlashDevelop

If you tried to compile my originally posted code with FlashDevelop, Flash Builder or any other IDE, it should work.

Original post:

Here's a basic setup for an embedded 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);
     }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...