Table of contents
- Very First Flex Preloader Customization
- Very First Flex Preloader Customization For Flex Builder 3/Flex SDK 3.2
Two days ago I needed to customize the very first preloader of a flex application (the one that is shown before finishing loading initialization classes of flex). My first impression was that it cannot be changed because I thought is something built in the Flash Player but at the end I found a way using a standard flash preloader (Flash 8 & Actionscript 2.0).
First we need to create a preloader class that will replace the standard one from the flex. That is CustomPreloader.as and is loaded in the application tag with
preloader="CustomPreloader"attribute.
The mxml file looks like this:
<mx:application xmlns:mx="http://www.adobe.com/2006/mxml"> layout="absolute" preloader="CustomPreloader"> <mx:box> borderStyle="solid" backgroundColor="#0F7494" borderColor="#0F7494" dropShadowEnabled="true" horizontalCenter="0" top="0"> <mx:swfloader> source="@Embed(source='test.swf')" width="550" height="400" /> </mx:box> </mx:application>
The test.swf file is embeded to make the flex application bigger to be able to test the preloader.
The CustomPreloader class is bellow:
package { import mx.preloaders.DownloadProgressBar; import flash.display.Sprite; import flash.events.ProgressEvent; import flash.events.Event; import mx.events.FlexEvent; import flash.display.MovieClip; public class CustomPreloader extends DownloadProgressBar { [Embed(source="preloader.swf", symbol="Preloader")] public var WelcomeScreenGraphic:Class; public var wcs:MovieClip; public function CustomPreloader():void { super(); wcs = new WelcomeScreenGraphic(); addChild(wcs); wcs.gotoAndStop(0); } public override function set preloader(preloader:Sprite):void { preloader.addEventListener(ProgressEvent.PROGRESS, onSWFDownloadProgress); preloader.addEventListener(Event.COMPLETE, onSWFDownloadComplete); preloader.addEventListener(FlexEvent.INIT_PROGRESS, onFlexInitProgress); preloader.addEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete); centerPreloader(); } private function centerPreloader():void { x = (stageWidth / 2) - (wcs.width / 2); y = (stageHeight / 2) - (wcs.height / 2); } private function onSWFDownloadProgress(event:ProgressEvent):void { var t:Number = event.bytesTotal; var l:Number = event.bytesLoaded; var p:Number = Math.round((l/t) * 19); var pForAmount:int = Math.floor(p * 5); wcs.gotoAndStop(p); wcs.amount_txt.text = String(pForAmount) + "%"; } private function onSWFDownloadComplete(event:Event):void { wcs.gotoAndStop(100); wcs.amount_txt.text = "100%"; } private function onFlexInitProgress(event:FlexEvent):void { wcs.gotoAndStop(100); wcs.amount_txt.text = " "; } private function onFlexInitComplete(event:FlexEvent):void { wcs.gotoAndStop(100); dispatchEvent( new Event(Event.COMPLETE)); } } }
Now the Preloader.swf and Preloader.fla file contains just the basic preloading stuff and even without actionscript. Important thing is that all thing in the preloader flash file are inside a symbol called “Preloader” symbol that is specified in the embed command
[Embed(source="preloader.swf", symbol="Preloader")] public var WelcomeScreenGraphic:Class;
because we want to be able to access things inside the flash preloader file.
Basic events are created and those access the flash preloader file. When loading is done we instruct the movie to go and stop at frame 100 even though there is no 100 frame but it will go to the last one available
wcs.gotoAndStop(100);
Here is our application in action (to review the preloader please empty your browser’s cache).
So… this is about it. You can find 2 archives: one with flash files and one with the flex application.
| ||
|
Tags: ActionScript, Events, Preloader
This post was written by Andrei Ionescu
Views: 35779










Interesting…
Thanks for posting!
Cyril
the fla´s samples files are corrupted, can you upload again
Thanks
Sergio, sorry to hear that you couldn’t use the files. I tested them and I managed to open them. All seems to be OK. I want to specify that the source files are Flash CS3 so you cannot open them with older version (or you can but the result may be unexpected). I hope you’ll be able to use them.
Thank you, i read at the beginning of your post: “(Flash 8 & Actionscript 2.0)” and was trying with flash 8, but now with CS3 all works fine, and i can understand what you do here. Its nice to make a custom preloader to flex.
Too you can change the defaults of flex preloader with action script like this:
package preloader
{
import mx.preloaders.DownloadProgressBar;
public class Loading extends mx.preloaders.DownloadProgressBar
{
public function Loading()
{
super();
downloadingLabel = "Please Wait..."
initializingLabel = "Initializing...";
}
}
}
sorry the english, im brazilian, tanks again and good lucky!
Thank for your comment Sergio and for pointing out that I specified Flash 8 instead of Flash CS3. I changed the article. Regarding your approach… I don’t think that will work for the first load for a big application because… to have loaded into the memory (and interpreted) the DownloadProgressBar class some big parts of the framework must be loaded and until then the normal flex preloader is loaded. With the approach explained in the article first is loaded the swf specified as preloader and then the framework so the flex preloader will be replaced.
This tutorial is actually vom Jesse Warden’s blog!
You might consider adding a backlink to:
http://jessewarden.com/2007/07/making-a-cooler-preloader-in-flex-part-1-of-3.html
Umm, this tutorial isn’t all from Jesse’s blog. It has some ideas from it (like the flash part) but mostly is done by myself and because of that I explained it better. Anyway, the backlink you requested is in your comment.
Kinda arrogant claiming you explained it better, isn´t it?
But you are right! Thanks for the tut, it really works nice and just the idea is great. Well done!
I find it too, good luck.
Best Regards Gartenmöbel
[...] For Flex Builder 3/Flex SDK 3.2Andrei Ionescu | 22.01.09 | Comment? Table of contentsVery First Flex Preloader CustomizationVery First Flex Preloader Customization For Flex Builder 3/Flex SDK [...]
I will post a demo of SWF support. It allows really rich pre-loaders in Flex. The cool thing is that if you use AVM SWF files( Flash 8- ) the SWF files esentially run in a separate VM thus they do not affect instanciation time for Flex SWF files. Flash 9 has 2 brains but in this case you can use them in parallel for a preloaded advantage.
Thanks; following your example was a breeze. I just used it to mimic the Facebook preloader for a Flex Facebook app:
Flex Facebook Preloader source files
Hello Tyler! Glad to hear that this tutorial helped you. Thanks for credits and trackback.
Hi , this is nice article for preloader . i have also found this article where i can use the external swf as preloader bar.Difference from this article is that the external swf bar progresses with loading of file rather than a continious loop of animation . seems nice check it here :
http://askmeflash.com/article_m.php?p=article&id=7
I use this solution to build up my new website. Thank you very much.
great article,
I have an application that is taking some 3 to 5 secs for initialization itself. So is it possible to show the percentage when it is initializing like download percentage