本文介绍了如何创建preloader在AS3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Flash应用程序是有点大了,所以我想嵌入一个preloader在我的应用程序,因此,谁能告诉我如何创建一个preloader在新的场景和加载其他场景后preloading完成?

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' andload another scene in after preloading completed?

在此先感谢!

推荐答案

更新:

选项1.闪存的IDE,一个SWF文件

要具有嵌入式preloader与Flash IDE编译时,你应该将你的文档类 code到FLA文件的第二帧(不包和类的构造函数,当然),并删除文档类。至于从项目属性文件。在第一帧,你应该把这样的code:

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();
}

在瑞士法郎装载,则进入到下一个帧和原来的应用程序初始化code应该被执行。这种行之有效的,如果你的方式组织你的项目中的大部分资产(图像等)是Flash IDE库中,并没有在第一帧上加载(你可以检查,在每个库项目的属性)。

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).

选项2的Flash IDE,两个SWF文件

另一种选择,因为已经建议另一位评论者,是让你的应用程序SWF,因为它是,并且创建另一个轻量级的SWF,将载入第一个。在第一帧中的$ C $的preloader.swf C:

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.

选项3不同的IDE,我的建议:的FlashDevelop

如果你试图编译我的原贴code用的是FlashDevelop,Flash Builder中或任何其他的IDE,它应该工作。

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

原帖:

下面是一个基本设置嵌入式preloader。你的文档类应该是这样的:

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类:

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);
     }
  }
}

这篇关于如何创建preloader在AS3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 16:17