如何在 AS3 中创建预加载器 [英] How to create Preloader in AS3

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

问题描述

我的 Flash 应用程序有点大,所以我想在我的应用程序中嵌入一个预加载器,那么谁能告诉我如何在新的场景"中创建预加载器和预加载完成后加载另一个场景?

提前致谢!

解决方案

更新:

选项 1. Flash IDE,一个 swf 文件

要在使用 Flash IDE 编译时嵌入预加载器,您应该将 Document Class 代码移动到 FLA 文件的第二帧(当然没有包和类构造函数),并删除 Document Class 来自项目属性的 .as 文件.在第一帧你应该放置这样的代码:

stop();//在预加载器帧处停止时间线this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);函数 onProgress(e:ProgressEvent):void {var percent:Number = Math.round(e.bytesLoaded/e.bytesTotal * 100);//用于更新预加载器图形的附加代码//..如果(百分比== 100)onLoaded();}函数 onLoaded() {this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);下一帧();}

一旦加载了 swf,它就会前进到下一帧,并且应该执行原始应用程序初始化代码.如果您以大多数资源(图像等)位于 Flash IDE 库中且未在第一帧中加载的方式组织您的项目,这将非常有效(您可以在每个库项目的属性中检查这一点).

选项 2. Flash IDE,两个 swf 文件

另一个评论者已经推荐的另一种选择是保持您的应用程序 swf 原样,并创建另一个轻量级 swf 来加载第一个 swf.第一帧preloader.swf的代码:

var loader:Loader = new Loader();loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);loader.load(new URLRequest("path/to/application.swf"));函数 onProgress(e:ProgressEvent):void{var percent:Number = Math.round(e.bytesLoaded/e.bytesTotal * 100);//用于更新预加载器图形的附加代码//..如果(百分比== 100)onLoaded();}函数 onLoaded():void{loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);var application:DisplayObject = loader.content;addChild(应用程序);}

有时,当您尝试从 Document Class 构造函数 等访问 stage 时,这种方法存在其他问题,但在大多数情况下,这应该可以解决问题.

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

如果您尝试使用 FlashDevelop、Flash Builder 或任何其他 IDE 编译我最初发布的代码,它应该可以工作.

原帖:

这是嵌入式预加载器的基本设置.您的 Document Class 应如下所示:

包{导入 flash.display.Sprite;[Frame(factoryClass='Preloader')]//你的预加载器的类名公共类 Main 扩展 Sprite {公共函数 Main() {//在里面}}}

预加载器类:

包{导入 flash.display.DisplayObject;导入 flash.display.MovieClip;导入 flash.events.ProgressEvent;导入 flash.utils.getDefinitionByName;公共类预加载器扩展了 MovieClip {公共函数预加载器(){//添加预加载器图形//查看加载进度this.loaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);}私有函数 onProgress(e:ProgressEvent):void{var percent:Number = Math.round(e.bytesLoaded/e.bytesTotal * 100);如果(百分比== 100){this.loaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);onLoaded();}}私有函数 onLoaded():void{下一帧();//进入下一帧var App:Class = getDefinitionByName("Main") as Class;//你的应用类addChild(new App() as DisplayObject);}}}

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!

解决方案

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆