views:

240

answers:

2

I'm creating a custom preloader for a Flex app and have noticed the following behavior: when loading, the progress bar goes to 100%, then down then back up, and so on until the app is finished loading.

When I put a trace in the dowloadprogress listener, I see that while the app is loading, both bytesLoaded and bytesTotal increase, but not necessarily at the same time.

Code:

private function onDownloadProgress(event:ProgressEvent):void {
        var loaded:int = event.bytesLoaded;
        var total:int = event.bytesTotal;
        trace(event.target,loaded,total);
        _starfield.progress = loaded/total;
    }

Output:

[object Preloader] 134276 134276
[object Preloader] 265348 285007
[object Preloader] 285007 285007
[object Preloader] 678223 1322116
[object Preloader] 809295 1322116
[object Preloader] 1322116 1322116
[object Preloader] 1322116 1322116
[object Preloader] 1387652 1584342
[object Preloader] 1791882 1791882
[object Preloader] 2293133 2293133
[object Preloader] 2362938 2362938
[object Preloader] 2362938 2362938
[object Preloader] 2362938 2362938

Why does bytesTotal change during load?

+3  A: 

As runtime shared libraries are started to be downloaded, the total can increase. You can learn a little more about it by reading the Preloader source code.

sdk\frameworks\projects\framework\src\mx\preloaders\Preloader.as

Here are some links to custom preloader samples that handle RSL's better than the default.

http://coding.bhirschmann.de/2008/03/20/preloader-for-flex-with-rsl-support/

http://www.leavethatthingalone.com/blog/index.cfm/2009/11/11/Flex4CustomPreloader

Sam
Okay, that seems plausible. What's the recommended way of calculating % loaded in this scenario?
justkevin
@justkevin, I added some links to custom preloader examples that handle RSL's better.
Sam
Both of those help to understand the RSL events, but unfortunately neither seems to solve the problem in my case. My app loads 6 RSLs of wildly varying size (in addition to the app itself), in succession. It doesn't appear that there's anyway to estimate how much of the total app has been (or remains to be) loaded until the last RSL starts.I may have to just fake it with a progress bar that randomly reduces itself by x% per frame.
justkevin
@justkevin, since you know the number of rsl's, just assign each to 1/6 of the total. It won't guarantee a correct progress bar, but it will guarantee a progressing progress bar.
Sam
A: 

or another way would be to break a preloader into 6 stages, with a preloader loading each component and running from 0 to 100%, then incrementing a number or "parts" loaded and displaying that on screen too

Alex