- We design and build extraordinary applications for companies looking to make the next great idea a reality.
- learn more
Custom Flex 3 Lightweight Preloader with source code
Here we have a demonstration to an age old problem in Flash (inherited in Flex) - the Preloader. I think that the Flex community has long ago grown bored with the default preloader, which I am very thankful to Adobe engineers for providing us in the first place. It's just that we keep seeing it over and over and over...
Another important thing that the default preloader naturally doesn't provide is some branding while the user is waiting for the loading to complete.
There are some great examples out there like Ted's and the one that I'm using as a base for this demonstration from Andrew.
Andrew's example allows us to have a lightweight preloader base that we can extend and solve the following problems that I think every preloader should:
- What is the status of loading - both numerically and graphically
- Some branding to show what are we waiting for that would involve some imagery, like a logo
- Possibility of actually making the preloader entertaining enough to keep the users attention
Problem in Flex more so than in Flash is keeping the preloader lightweight. So we have to achieve the visual appeal, animation, some basic text rendering without all the convenient native Flex components that would make the delay before seeing the preloader too long. So we have to keep it simple. Kudos to Andrew for publishing a great way to do this.
I would like to point out a couple things:
1. So we want to show the percentage loaded, right? Well, the Label component just won't work out. Label comes with following inheritance:
Label -> UIComponent -> FlexSprite -> Sprite -> DisplayObjectContainer -> InteractiveObject DisplayObject -> EventDispatcher -> Object
That is a good part of the Flex framework that we want to avoid for the preloader. Instead, because we just want to render the text, we can use the TextField object which comes with following inheritance:
TextField -> InteractiveObject -> DisplayObject -> EventDispatcher -> Object
And another thing is that we want to avoid any custom fonts in the preloader because that will surely blow it up.
2. Any imagery that we want to show up should naturally be in the single digit kilobytes to begin with. Photoshop offers a lot of ways to optimize the size of the image.
Than again, we don't want to use the convenient Image component because it comes with the following inheritance:
Image -> SWFLoader -> UIComponent -> FlexSprite -> Sprite -> DisplayObjectContainer -> InteractiveObject -> DisplayObject -> EventDispatcher -> Object
Instead, we can fill up a DisplayObject that comes with following inheritance:
DisplayObject -> EventDispatcher -> Object
To do this you:
- embed your small image in the preloder like so:
[Embed("Assets/Pathfinder_Logo_Blue.png") ]
[Bindable] public var Logo:Class;
- pass the Logo class to DisplayObject and add it to stage, like so:
var b:DisplayObject=new Logo(); addChild(b);
and then set it's size and position parameters.
We only want to show the image pixel, so this will do. We can still make our logo DisplayObject available throughout the whole preloader class so we can play with it a little, like making it follow the progress bar by changing it's X parameter.
The rest you can pick up from the source code, available here.
You can see the example here.
Again, thanks to Andrew, Ted and Adobe Flex Team.
Topics: Flex, lightweight, preloader
Comments: 7 so far
Leave a comment
About Pathfinder
Recent
- Firefox Plugin Malware ‘Trojan.PWS.ChromeInject.A’
- Pathfinder releases version 1 of the its Flash Platform microsite (codename Mica)
- Pimp my Rails: Five Plugins & Gems to Make Rails Better
- iPhone: Using Pre-processor Directives for Device Testing
- Subtle OpenGL Projection Matrix Difference Between iPhone Simulator and Device
- App Security: Throw Out the Org Chart!
- Pimp my jQuery: Five plugins to replace the features Prototype and Scriptaculous users expect
- Thanksgiving 2008: What We’re Thankful For (In Rails)
- iPhone SDK: Testing with TextMate & GTM
- GWTQuery - JQuery-like Syntax in GWT
Archives
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
- June 2007
- May 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- March 2006



Very nice !!Thank you !
Comment by roamer, Friday, August 22, 2008 @ 5:52 am
Is it possible to load a swf file for preloading and call a function in that swf.
For instance, I would like to use a kind of speedometer for preloading. I’ve created a swf that has a ’setPercentageLoaded(percentage)’ function in the first frame of the movieclip. This function rotates the dial of the speedometer based on the percentage loaded.
I’ve been trying to accomplish this, but embedding swf files causes the function to be unreachable.
Any help would me most appreciated.
Regards,
Sander
Comment by Sander, Friday, August 29, 2008 @ 10:41 am
Hi Sander,
To the best of my knowledge it is not possible call a method within a swf file while it’s loading because it doesn’t exist yet. Having the preloader in the swf that you are trying to load defies the purpose because your swf would need to load before showing your the preloader.
You do not need to do that at all because there are other ways around this.
In my experience, it is best to keep the preloader extracted so you can call on it anytime for anything and reuse it.
Your scenario could look something like this:
- You have a base file. It’s a default MXML.
- In your project, you have a component that will act as your preloader and is loaded together with your application.
- You have an external swf that you want to load after your app has loaded.
From that starting point we go to code in base file (imports not included):
//loader that will track the progress
private var myLoader:Loader;
//your custom preloader component
public var myPreloader:Speedometer;
//create your preloader and call the loading of the external swf file
private function getSwf():void
{
//create your new preloader component
myPreloader = new Speedometer();
//position it where ever you want and set any other parameters you want
myPreloader.x = 100;
myPreloader.y = 100;
//add your preloader to stage
addChild(myPreloader)
//create your loader
myLoader = new Loader();
//create a new URLRequest to use as with Loader
var mySwfURL:URLRequest = new URLRequest(”URL_of_my_swf.swf”);
//attach a listener to loader so you can track the progress using it
myLoader.addEventListener(ProgressEvent.PROGRESS, trackProgress);
//start loading
myLoader.load(mySwfURL);
}
//trackProgress is attached to the listener and is being passed “progress” parameters in event parameter
private function trackProgress(event:ProgressEvent):void
{
//this is where you can pass the progress to your custom preloader
myPreloader.setPercentageLoaded(event.bytesLoaded / event.bytesTotal * 100 );
//if the swf has loaded, remove your listener and custom preloader from the stage
if(event.bytesLoaded == event.bytesTotal)
{
myLoader.removeEventListener(ProgressEvent.PROGRESS, trackProgress);
removeChild(myPreloader)
}
}
Comment by Sasha Dzeletovic, Tuesday, September 2, 2008 @ 2:14 pm
Hi,
Thanks for proving such a beatiful component code,I am trying to use this preloader for AIR application its not working and its perfectly working for Web app’s.Pls clarify me that preloder concept will work for AIr,if possible provide some sample.
Thanks & Regards
Kalluru
Comment by kalluru, Wednesday, November 12, 2008 @ 8:58 am
Hi Sasha,
Thank you for sharing your solution with us!
Can we use your preloader with our logo in our applications? Are there any licenses or restrictions?
Comment by Darina, Wednesday, November 26, 2008 @ 2:41 pm
Hi Darina,
Thanks! You can use this preloader in your applications without any restrictions whatsoever. Also feel free to change it, extend it or whatever else that comes to your mind. Pathfinder logo is copyrighted though
Comment by Sasha Dzeletovic, Wednesday, December 3, 2008 @ 3:33 pm
Thanks Kalluru,
I haven’t tried this in AIR but right of the top of my mind I would think something similar could work. Main difference that I foresee is that in AIR we are not loading but mostly initializing. I will get to it when time permits and update to this post on that topic.
Cheers!
Comment by Sasha Dzeletovic, Wednesday, December 3, 2008 @ 3:36 pm