Article
Display Download Status In Your Flash Preloader
A Little Bit Extra
Well, at the very start, I said that one of the possible feedback displays we could use was the estimated time remaining. It will require just a little more code, so let’s do it.
Another handy function in Flash is 'getTimer()' which returns the number of milliseconds since the movie started. Just as I'm not rapt in bytes, I'm also not that fond of milliseconds. I'm going to use (getTimer()/1000), which will give the value in seconds instead.
In the frame 2 code, let’s calculate the number of kBytes per second -- you could display that, too, if you like.
kBytesSec = kBytesLoaded/(getTimer()/1000);
It's that's simple: the number of kBytes loaded, divided by the number of seconds it's taken.
And now, the time remaining:
timeRemaining = kBytesRemaining / kBytesSec;
Another simple one. We divide the amount remaining by the newly calculated download speed.
So our display message might be ...
progress = Math.floor(timeRemaining) add " seconds remaining";
Hey that’s cool! Now your visitors know if there really is enough time to put the kettle on before your movie starts!
So, don't leave your visitors twiddling their thumbs, grinding their teeth, and potentially abandoning the download altogether. Tell them what's going on -- it's that easy! Keep them informed, keep them happy.
And that’s just the beginning. There’s so much more you could do. Check out other movieClip properties you can access, play with the rotation or position values, get in there and mess around, and have some fun!
Problems, Workarounds, Other Bits ‘n’ Pieces
The preloader we’ve built here will work well for most Flash movies. However, there are situations where it will not perform as you might like.
Loaded Submovies
The values returned by getBytesLoaded and getBytesTotal only apply to the main movie.
If you’re loading movies and sounds into your main movie, these bytes are not counted by the preloader, and as soon as the main movie is loaded, it will continue regardless of any external files that are still loading.
If you had a submovie being loaded into level 1 and wanted it, as well as the main movie, to preload, you could do something along the following lines...
kBytesLoaded = (_parent.getBytesLoaded() +
_level1.getBytesLoaded()) / 1024;
kBytesTotal = (_parent.getBytesTotal () +
_level1.getBytesTotal ()) / 1024;
Linked Assets
When a movie's library contains items with linkage, the linked objects, and any objects they depend on, are exported in the first frame (this can be avoided in MX -- see below).
As Flash doesn’t display a frame until it has loaded, this can produce a 'stalled' effect when loading the movie (because the first frame can obviously get quite large).
Files using 'attachSound()' and 'attachMovie()' have linked assets. Linkage can also be common in Flash components.
Flash MX introduced a way of linking assets that does not place them in frame 1 of your movie. By default, linked assets are still written into frame one, but this can be avoided.
Uncheck the 'export in first frame' option for the linked asset(s). Doing this will prevent the item from being exported, unless it’s manually placed on the stage. You’ll then need to manually place the item in a frame after your preloader and before any actionScript that calls the attach function.
Chances are, you don’t want these items manually placed, which is why you're using attach functions in the first place, so you'll probably want to place them somewhere offscreen, or on a frame that doesn't ever get played.
This can all get a little laborious (particularly if you're using components) as these often have numerous exported subelements, which you will also have to adjust and manually relocate.
Resources and Links
Got problems? Here are some links that could ease your troubles.
ActionScript.org Flash community forums, movies, tutorials.
http://www.actionscripts.org
'AssetMover'. Utility that moves linked assets to a specified frame.
http://www.flexidev.com
Flashcoders/Flashnewbie (and other lists). Some great mailing lists. Things can get pretty hardcore on the Flashcoders list (and will fill up your inbox. You might want to subscribe to the digest!).
http://chattyfig.figleaf.com
FlashKit. Flash community sites, forums, tutorials, movies. Stuff for everyone, from designers to code junkies.
http://www.flashkit.com
Neave’s Webgames. Completely off-topic, but when you need to kill some time, this site has some classic games recreated in Flash (completely free -- downloadable .fla’s and all). Hey, wait a minute, don’t get distracted -- make your preloader first!
http://www.neave.com/webgames/
And, on my desk, amongst other volumes, is “ActionScript for Flash MX – The Definitive Guide – 2nd Edition” by Colin Moock. Over a thousand pages of ActionScript information. It’s not really a tutorial/how-to book, but it’s a very solid reference.