Building Custom Loaders in Flex – taterboy
June 5th, 2010 | Filed under: ActionScript 3, Animation, Flash, Flex, Tutorials

In an earlier post I talked all about building Flex UI components with Flash. This example shows how to create a custom loader component, in Flash or Flex, and use it to replace the standard Flex progress bar.


Get Adobe Flash player




The Base Preloader:
I found a preloader class online that implemented the IPreloaderDisplay, this is required to build a custom preloader. You can find out more about building your own here. I took the class I found online and made it generic so I could use it as a base class for all my future preloaders.

The Code (BasePreloader.as):

?View Code ACTIONSCRIPT
package com.hdi.loaders
{
  import flash.display.Sprite;
  import flash.events.Event;
  import flash.events.ProgressEvent;
  import flash.display.MovieClip;
  import mx.events.FlexEvent;
  import mx.preloaders.IPreloaderDisplay;
  import mx.preloaders.Preloader;
 
  public class BasePreloader extends MovieClip implements IPreloaderDisplay
  {
 
 	private var _preloader:Preloader;
	public var onUpdate:Function = null;
	public var onInit:Function = null;
 
    public function BasePreloader(){
		super();
		//trace("loader started");
 
    }
 
    public function get backgroundAlpha():Number{
		return 0;
    }
 
    public function set backgroundAlpha(value:Number):void{
    }
 
    public function get backgroundColor():uint{
		return 0;
    }
 
    public function set backgroundColor(value:uint):void{
    }
 
    public function get backgroundImage():Object{
		return null;
    }
 
    public function set backgroundImage(value:Object):void{
    }
 
    public function get backgroundSize():String{
 		return null;
    }
 
    public function set backgroundSize(value:String):void{
    }
 
    public function set preloader(obj:Sprite):void{
		_preloader = obj as Preloader;
		_preloader.addEventListener(ProgressEvent.PROGRESS, progressEventHandler);
		_preloader.addEventListener(FlexEvent.INIT_COMPLETE,initCompleteEventHandler);
    }
 
    public function get stageHeight():Number{
		return 0;
    }
 
    public function set stageHeight(value:Number):void{
    }
 
    public function get stageWidth():Number{
		return 0;
    }
 
    public function set stageWidth(value:Number):void{
    }
 
   public function initialize():void{
		this.x = stage.stageWidth/2 - this.width/2;
		this.y = stage.stageHeight/2 - this.height/2;
		if(onInit != null){
			onInit();
		}
    }
 
    private function progressEventHandler(eo:ProgressEvent):void{
    	if(onUpdate != null){
      		onUpdate(Math.round((eo.bytesLoaded / eo.bytesTotal )*100));
     	}
    }
 
    private function initCompleteEventHandler(eo:FlexEvent):void{
     	dispatchEvent(new Event(Event.COMPLETE));
    }
  }
}


The Flash Visual Component:
The loader animation and TextField were built in Flash. I added a couple functions to the MovieClip’s timeline before converting the symbol to a Flex Component. The symbol name for the loader component is “LoaderRing”.

The Code:

?View Code ACTIONSCRIPT
// adjusting colors accepts uint
function set ringColor(col:uint):void{
	var colors:ColorTransform = new ColorTransform();
	colors.color = col;
	ring.transform.colorTransform = colors;
}
 
// tell the animation to start or stop
function playAnimation(val:Boolean){
	if(val){
		ring.play();
	}
	else{
		ring.stop();
	}
}
 
// send a string to the TextField
function updateText(str:String):void{
	ldrTxt.text = str;
}
 
stop();




Putting It All Together:
With the base class tucked away and the SWC exported from Flash and placed in the libs folder, we are ready to build the actual progress loader. This will be a subclass of BasePreloader.as, from above, and control all the visual aspects of the loader. You can build anything you want, load images, load swfs or component artwork from Flash. We are showing the latter.

The Code (CustomLoaderMC.as):

?View Code ACTIONSCRIPT
package
{
	// imports
	import com.hdi.loaders.BasePreloader;
	import flash.filters.GlowFilter;
 
	public class CustomLoaderMC extends BasePreloader
	{
		// UIMovieClip class from Flash, via libs folder
		private var ldr:LoaderRing;
 
		public function CustomLoaderMC()
		{
			// filters are cool
			var filter:GlowFilter = new GlowFilter(0xD75F0F,1,12,12,1.8,2);
 
			// new instance of Flash UIMovieClip
			ldr = new LoaderRing();
			// update colors - snazzy
			ldr.ringColor = 0xD75F0F;
			// apply filters
			ldr.filters = [filter];
			// offset x and y, centering the Flash Component
			ldr.x = ldr.width/2;
			ldr.y = ldr.height/2;
			//add child to display list
			this.addChild(ldr);
 
			// hookup properties from super class
			this.onUpdate = updateLoader;
			this.onInit = init;
 
			super();
		}
 
		// on init function  - reset percentage label
		public function init():void{
			ldr.updateText("0%");
		}
 
		// on progress function - update percentage label
		public function updateLoader(perc:Number):void{
			ldr.updateText(perc + "%");
 
			// if progress bar was precent, you would update that here.
			//progressBar.scaleX = perc/100;
 
			// the ring animation is made of of 10 frames, we could progress 			
			// each frame of the animation as the percentage increases.
			// ldr.ring.gotoAndStop(Math.ceil(perc/10));
 
		}
 
	}
}


Tip: MXML components will not work as preloaders.

Adding The Preloader To Your Flex App:
Once you have your preloader built and looking good, the last step is to assign it to the preloader attribute of the Application’s MXML tag.

The Code:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:local="*" preloader="CustomLoaderMC" >


Download the source here.
The extra file “CustomLoaderDisplay.mxml” is just for showing off the loader, demo above.

Technorati Tags: , , , , , , , ,

Other Related Articles:

|

2 Comments »

  1. [...] Building Custom Loaders in Flex | Taterboy.com: Graphics … [...]

    Pingback by Sunset Portrait Photography Tips | ModernPhotography.info — June 12, 2010 @ 10:57 am

  2. [...] Pre-Loading In Flex: See my previous post on Building Custom Loaders in Flex [...]

    Pingback by How to Build an Engaging Preloader in Flash : Flashflex.com — July 29, 2010 @ 1:09 am

RSS feed for comments on this post. TrackBack URL

Leave a comment