1

Like every programmer I´m facing a tricky challenge, and I hope someone could share with me some knowledge.   I'm creating a game, and for each game component(such as audio, video, pictures), I´ve created a swf file(library). At the main file, I am trying to load my swf files using the Loader class.   I would like to make a preloader and I want to calculate the percentage of bytes loaded from of all of my swf files. I´ve realized that loading 3 loaders at the same time makes my game slow. I tried to upload one file at a time, but I don´t know how to catch the bytes loaded from my files without using ProgressEvent(Here I can access bytesTotal variable).   Does anyone faced a similar problem and could share a tip or a link?     Regards and Good Friday!

4

2 回答 2

0

我强烈推荐你使用GreenSock 的 LoaderMax库。它简化并增强了整个过程。它易于设置且体积小。

import com.greensock.*;
import com.greensock.loading.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.display.*;

// Create a LoaderMax named "gameQueue" and bind onProgress, onComplete and onError listeners
var queue:LoaderMax = new LoaderMax({name:"gameQueue", onProgress:progressHandler, onComplete:completeHandler, onError:errorHandler});

// Append several loaders
queue.append( new XMLLoader("xml/data.xml", {name:"xmlDoc"}) );
queue.append( new ImageLoader("img/photo1.jpg", {name:"photo1", estimatedBytes:2400, container:this, alpha:0, width:250, height:150, scaleMode:"proportionalInside"}) );
queue.append( new SWFLoader("swf/child.swf", {name:"childClip", estimatedBytes:3000, container:this, x:250, autoPlay:false}) );
queue.append( new MP3Loader("mp3/audio.mp3", {name:"audio", repeat:2, autoPlay:true}) );

// Run
queue.load();

function progressHandler(event:LoaderEvent):void {
    trace("progress: " + event.target.progress);
}

function completeHandler(event:LoaderEvent):void {
    var image:ContentDisplay = LoaderMax.getContent("photo1");
    TweenLite.to(image, 1, {alpha:1, y:100});
    trace(event.target + " is complete!");
}

function errorHandler(event:LoaderEvent):void {
    trace("error occured with " + event.target + ": " + event.text);
}

您可以显示单个加载程序或加载程序组的进度。

为了准确 计算一组加载器的整体进度bytesTotal,了解每个子加载器的总文件大小 ( ) 很重要。但是如果一些文件还没有开始加载,你的加载器怎么可能知道它们的大小呢?

没有TCP packet,它将在进度开始时通知您最终大小。

记住:就像一个流。在文件准备好之前,您永远不会知道最终大小!

new ImageLoader("1.jpg", {estimatedBytes:26000});

一个简单的解决方法:

  1. 生成一个列表,其中包含每个可预加载元素的 url 和总大小。中的一些自动化来做到这一点(例如),检查这个SO answer

  2. 使用 LoaderMax 预加载 xml/json 文件列表并在 AS3 中解析它。

  3. 从 xml 中加载所有文件并添加正确的文件大小。而已。

您可以在此处找到完整的 AS3 文档。

于 2013-08-02T18:55:47.817 回答
-1

收到大小信息后立即开始加载文件、获取文件大小并关闭流。

然后根据需要一一加载文件。

或者编译你的主 swf 并合并到代码库中(如果这适合你)。

于 2013-08-02T19:56:42.527 回答