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!
-
You need get the total bytes of swf loaded? – Gaston Flores Aug 02 '13 at 18:56
2 Answers
I strongly recommend you the GreenSock's LoaderMax library. It simplifies and enhances the entire process. It's easy to setup and small.
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);
}
You can display the progress of individual loaders or groups of loaders.
In order to accurately calculate the overall progress of a group of loaders, it’s important to know the total file size (bytesTotal
) of each child loader. But if some files haven’t started loading yet, how could your loader possibly know their sizes?
There's no TCP packet
, that will inform you about the final size when the progress starts.
Remember: get is like a stream. You'll never know the final size, until the file is ready!
new ImageLoader("1.jpg", {estimatedBytes:26000});
A simple workaround:
Generate a xml or json list, that contains the url and the total size of each preloadable element. You can do this with some automation in php (for example), check this SO answer.
Preload the xml/json file-list with LoaderMax and parse it in AS3.
Load all files from within the xml and add the correct file size. That's it.
You can find the full AS3 documentation here.
-
I´ve tested it yesterday, cept0. But realized when it loads multiple files, it doesnt know what size all my loaders have. It loads the first file till it reaches 100%, next, the second file, etc. I could see it using debuger. Is there a way LoaderMax load all of the files without restart "progress value" on ProgressEvent method? Sorry my bad english. – rennanreis Aug 02 '13 at 19:36
Start to load your files, get file size and close the stream immediately after you received size info.
And then load your files one by one as you wanted.
Or compile your main swf with merged into code libraries(if this is suitable for you).

- 1,303
- 9
- 18
-
How can I compile my main swf files merged into code libraries, user1875642? – rennanreis Aug 04 '13 at 15:13
-
If your libraries contain only assets(I guess this is your case), you can include them into your main swf with embedding(http://actionscriptexamples.com/2008/10/26/embedding-images-into-a-flash-document-using-the-embed-metadata/). If your libraries contain some code that you use, then there's a compiler option to merge libs into code(http://help.adobe.com/en_US/flash/cs/using/WS3e7c64e37a1d85e1e229110db38dec34-7fa4a.html). – user1875642 Aug 04 '13 at 18:18