I’ve blogged on this before but here’s an updated version of a custom preloader class that extends Sprite and loads in a MovieClip from a SWC to use as the animation and display the load progress.
- package com.cravens.nr.preloader
- {
- import com.cravens.nr.model.Config;
- import flash.display.GradientType;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.ProgressEvent;
- import flash.events.TimerEvent;
- import flash.geom.Matrix;
- import flash.utils.Timer;
- import mx.events.FlexEvent;
- import mx.preloaders.IPreloaderDisplay;
- public class NRPreloader extends Sprite implements IPreloaderDisplay
- {
- protected var loadingPanel:BusyPanel;
- protected var timer:Timer;
- public function NRPreloader()
- {
- super();
- }
- //------------------------------------------------------------------------------
- public function set preloader(preloader:Sprite):void
- {
- preloader.addEventListener(ProgressEvent.PROGRESS, handleProgress);
- preloader.addEventListener(Event.COMPLETE, handleComplete);
- preloader.addEventListener(FlexEvent.INIT_PROGRESS, handleInitProgress);
- preloader.addEventListener(FlexEvent.INIT_COMPLETE, handleInitComplete);
- }
- //------------------------------------------------------------------------------
- public function initialize():void
- {
- var matrix:Matrix = new Matrix(0,1,1,0,0,0);
- graphics.beginGradientFill(GradientType.LINEAR,[0x000000,0x89265a],[1,1],[125,205],matrix);
- graphics.drawRect(0,0,Config.VIEWPORT_WIDTH,Config.VIEWPORT_HEIGHT);
- loadingPanel = new BusyPanel();
- try
- {
- loadingPanel.heading_txt.text = "LOADING ... PLEASE WAIT";
- loadingPanel.message_txt.text = "0/100%";
- }
- catch(e:Error)
- {
- throw new Error("Asset does not contain the relevant textField objects");
- }
- finally
- {
- loadingPanel.x = Config.VIEWPORT_CENTER_X-150;
- loadingPanel.y = Config.VIEWPORT_CENTER_Y;
- addChild(loadingPanel);
- }
- }
- //------------------------------------------------------------------------------
- // Define empty event listeners.
- private function handleProgress(event:ProgressEvent):void
- {
- var prog:Number = Math.ceil((event.bytesLoaded/event.bytesTotal)*100);
- loadingPanel.message_txt.text = prog+"/100%";
- }
- //------------------------------------------------------------------------------
- private function handleComplete(event:Event):void
- {
- loadingPanel.message_txt.text = "LAUNCHING";
- }
- //------------------------------------------------------------------------------
- private function handleInitProgress(event:FlexEvent):void
- {
- }
- //------------------------------------------------------------------------------
- private function handleInitComplete(event:Event):void
- {
- timer = new Timer(500,1);
- timer.addEventListener(TimerEvent.TIMER, dispatchComplete);
- timer.start();
- }
- //------------------------------------------------------------------------------
- private function dispatchComplete(event:TimerEvent):void
- {
- timer.removeEventListener(TimerEvent.TIMER, dispatchComplete);
- timer = null;
- dispatchEvent(new Event(Event.COMPLETE));
- }
- //------------------------------------------------------------------------------
- // Implement IPreloaderDisplay interface
- public function get backgroundColor():uint
- {
- return 0;
- }
- //------------------------------------------------------------------------------
- public function set backgroundColor(value:uint):void
- {
- }
- //------------------------------------------------------------------------------
- public function get backgroundAlpha():Number
- {
- return 0;
- }
- //------------------------------------------------------------------------------
- public function set backgroundAlpha(value:Number):void
- {
- }
- //------------------------------------------------------------------------------
- public function get backgroundImage():Object
- {
- return undefined;
- }
- //------------------------------------------------------------------------------
- public function set backgroundImage(value:Object):void {
- }
- //------------------------------------------------------------------------------
- public function get backgroundSize():String {
- return "";
- }
- //------------------------------------------------------------------------------
- public function set backgroundSize(value:String):void {
- }
- //------------------------------------------------------------------------------
- public function get stageWidth():Number
- {
- return Config.VIEWPORT_WIDTH;
- }
- //------------------------------------------------------------------------------
- public function set stageWidth(value:Number):void
- {
- }
- //------------------------------------------------------------------------------
- public function get stageHeight():Number
- {
- return Config.VIEWPORT_HEIGHT;
- }
- //------------------------------------------------------------------------------
- public function set stageHeight(value:Number):void
- {
- }
- }
- }
Also, notice I’ve created a simple grad background using the Sprite graphics …
-
var matrix:Matrix = new Matrix(0,1,1,0,0,0);
-
graphics.beginGradientFill(GradientType.LINEAR,[0x000000,0x89265a],[1,1],[125,205],matrix);
-
graphics.drawRect(0,0,Config.VIEWPORT_WIDTH,Config.VIEWPORT_HEIGHT);
This is the best way to do things because remember, the framework has not loaded while the preloader is displayed so you need to keep things lightweight and simple.
Also, the Matrix object rotates the grad so it runs vertically.
Have fun.


Recent Comments