Flex & Tutorials 19 Mar 2009 01:35 pm

A Flex Preloader for the St. Patrick’s

In a Flex application, the pre-loader is the first thing your users see. It’s important to have a eye-catching preloader to establish your branding. This tutorial shows you how to create an animated preloader in a few easy steps. It is intended for Flex developers with little experience in Flash and Illustrator.

Here is a quick demo of what we are about to build.

Before we begin, you can download all the source files used in this tutorial to follow along.

We just had St. Patrick’s Day earlier this week, so we will do a beer-themed custom preloader. Look for the “beer_glass.ai” file in the source zip. It has a simple vector illustration of an empty pint, and full pint, and the branding text for FlexLive.net.

beer_glass.ai

We will use Flash CS4 to turn the vector assets into a short preloader animation. To begin, create an empty Flash file for ActionScript 3.

new flash file

In Flash CS4, go to File > Import > Import to Stage, and select the Illustrator asset (beer_glass.ai). You should be greeted by an import screen as follows. Make sure the “Set stage size to same size…” is selected, and click OK to proceed.

import screen

Once all the vector assets are imported to the Flash stage, press ctrl+a to make sure all objects are selected. Then right click on the selected objects and choose “Convert to Symbol”. In the convert symbol dialog, name the symbol “preloader”, and enable “Export for ActionScript”. The symbol name will be referenced later from your Flex application. The convert symbol dialog should look as follows,

convert to symbol

After you are done with the convert symbol dialog, the assets should be grouped into a single object on your Flash stage.

new symbol

Now we can start with the fun animation work. Double click on the graphic asset to start editing.  Notice when you first enter the edit mode, there are three objects selected: the empty glass, the full glass, and the decor text. Make sure all objects are selected, right click, and select “Distribute to Layers”.

distribute to layers

Now you should have four layers. We should rename them so they are easier to work with. You can rename a layer by double clicking on its label.

Rename “layer 1″ TO “mask”, “layer 2″ to “glass_full”, “layer 3″ to “glass_empty”, and “layer 4″ to “text”. Re-arrange the layers by dragging them up and down. The final layering should look as follows,

layers

Now select the empty glass and full glass objects, and align them to the left and bottom. The result should look as follows,

aligned

With the “mask” layer selected, set the stroke color to “none” and fill color to a bright green. Ensure “Object Drawing” is enabled, and select the rectangle tool (see screenshot below).

tool selection

Draw a rectangle under that is at least as wide as the glass graphics.

mask step 1

Right click on the frame 20 of the mask layer, and select “Insert Keyframe”.

mask step 2

Use the “Free Transform Tool” (Q), resize the green rectangle to cover the entire glass.

mask step 3

Right click on the mask timeline and select “Create Shape Tween”. For the other three layers, right click on frame 20 of each and select “Create Keyframe”. The final timeline should look as follows,

mask step 4

For the final step, right click on the mask layer and select “Mask”.

mask step 5

Preview the animation and save the Flash file. Go to File >Publish to create the .swf asset file.

In Flex Builder, we will create a custom preloader class by extending the “DownloadProgressBar” class,


public class AppPreLoader extends DownloadProgressBar {...}

We will embed the preloader animation as follows,

[Embed(source="assets/beer_glass.swf", symbol="preloader")]
private var PreloaderAsset:Class;

We can instantiate the preloader animation asset with the MovieClip class,

private var _mov:MovieClip;
_mov = new PreloaderAsset();

When extending the “DownloadProgressBar”, we need to extend the preloader setter to insert the progress handles,

public override function set preloader(preloader:Sprite):void
{
preloader.addEventListener(ProgressEvent.PROGRESS, onSWFDownloadProgress);
preloader.addEventListener(FlexEvent.INIT_COMPLETE, onFlexInitComplete);
}

The preloader progress handler will be driving the animation frame by frame. Recall from ealier, our animation had 20 frames.

private function onSWFDownloadProgress(evt:ProgressEvent):void
{
var p:int = (evt.bytesLoaded/evt.bytesTotal) * 20;
_mov.gotoAndStop(p);
}

Finally, we will set the custom preloader class in the root application node,

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" preloader="net.flexlive.skin.AppPreLoader" >

That concludes our tutorial. All the source is available in this zip file. Feel free to leave a comment for questions.

Need to bring your Flex project up to speed? Zee Yang is a freelance Flex developer with deep understanding of architecture and user experience. You can reach him at zee.yang@gmail.com.

9 Responses to “A Flex Preloader for the St. Patrick’s”

  1. on 20 Mar 2009 at 1:49 pm 1.Ryan Stewart said …

    Hah! Awesome. You should have it going in the other direction though ;)

    =Ryan
    ryan@adobe.com

  2. on 26 Mar 2009 at 9:15 pm 2.Flash Speaks ActionScript Community » Blog Archive » A Flex Preloader for St. Patrick’s Day said …

    [...] via Flex Live [...]

  3. on 21 Apr 2009 at 8:01 am 3.Cristian Rotundu said …

    Very nice and useful tutorial! (and inspiring too :D )

  4. on 22 May 2009 at 5:49 am 4.Jelina said …

    wow this is really nice preloader ,
    but i was looking for flex preloader . Found a nice one at this link :
    http://askmeflash.com/article_m.php?p=article&id=7

  5. on 11 Jun 2009 at 9:34 am 5.Mike Chablis said …

    that rocks!
    and it works on Flex Jelina!
    but it would be hard to code animation directly in AS3

  6. on 18 Jun 2009 at 7:39 am 6.Andy Diggens said …

    Excellent work Zee, is there any way to incorporate the progress labels as well?

  7. on 27 Jul 2009 at 7:30 pm 7.Anna Filina said …

    Thanks for including the Flex sources. You saved me quite some time.

  8. on 01 Dec 2009 at 4:29 pm 8.Chris said …

    Nice step by step tutorial, thanks for sharing!

  9. on 15 Jan 2010 at 9:40 am 9.raghavendra said …

    Nice to see this i saw demo its good.. good

Trackback This Post | Subscribe to the comments through RSS Feed

Leave a Reply