3 import flash
.display
.*;
8 import mx
.events
.FlexEvent
;
9 import mx
.preloaders
.*;
10 import mx
.preloaders
.IPreloaderDisplay
;
11 import flash
.filters
.GlowFilter;
17 public class Preloader
extends Sprite implements IPreloaderDisplay
19 private var txtLoading
:TextField;
20 private var graphicLoader
:Sprite = new Sprite();
21 private var bgcolor
:Sprite = new Sprite();
22 private var c
:Number = 0; // angle.
23 private var percentDone
:Number = 0;
25 public function Preloader
()
33 // Specify the event listeners.
34 public function set preloader
(preloader
:Sprite):void
36 // Listen for the relevant events
37 preloader
.addEventListener
(Event.ENTER_FRAME
, onFrame
);
38 preloader
.addEventListener
(ProgressEvent.PROGRESS
, handleProgress
);
39 preloader
.addEventListener
(Event.COMPLETE
, handleComplete
);
40 preloader
.addEventListener
(FlexEvent
.INIT_PROGRESS
, handleInitProgress
);
41 //preloader.addEventListener(FlexEvent.INIT_COMPLETE, handleInitComplete);
44 // Initialize the Loader control in the override
45 // of IPreloaderDisplay.initialize().
46 public function initialize
():void
48 //stage.scaleMode = StageScaleMode.NO_SCALE;
49 //stage.align = StageAlign.TOP_LEFT;
52 bgcolor
.graphics
.clear
();
53 bgcolor
.graphics
.beginFill
(0x000000, 1);
54 bgcolor
.graphics
.drawRect
(0, 0, stageWidth
, stageHeight
);
55 addChildAt
(bgcolor
, 0);
58 addChild
(graphicLoader
);
59 graphicLoader
.width
= stage
.stageWidth
;
60 graphicLoader
.height
= stage
.stageHeight
;
63 stage
.addEventListener
(Event.RESIZE
, resizeBGWithStage
);
65 txtLoading
= new TextField();
66 txtLoading
.text
= "0%";
68 var tf
:TextFormat = new TextFormat();
71 txtLoading
.setTextFormat
(tf
);
72 txtLoading
.textColor
= 0xffffff;
75 private function onFrame
(e
:Event):void
80 private function resizeBGWithStage
(e
:Event):void
84 bgcolor
.width
= stageWidth
;
85 bgcolor
.height
= stageHeight
;
86 bgcolor
.graphics
.beginFill
(0x000000, 1);
87 bgcolor
.graphics
.drawRect
(0, 0, stageWidth
, stageHeight
);
91 graphicLoader
.width
= stageWidth
;
92 graphicLoader
.height
= stageHeight
;
94 txtLoading
.x
= (stageWidth
/ 2) - (txtLoading
.width
/ 2);
95 txtLoading
.y
= (stageHeight
/ 2) - (txtLoading
.height
/ 2);
102 private function handleProgress
(e
:ProgressEvent):void
104 txtLoading
.text
= ((e
.bytesLoaded
/ e
.bytesTotal
) * 100) + "%";
106 c
= Math.round
((e
.bytesLoaded
/ e
.bytesTotal
) * 100 * (-1.84));
109 private function draw
():void {
112 var glow
:GlowFilter = new GlowFilter();
113 glow
.color
= 0xffffff;
118 graphicLoader
.graphics
.clear
();
120 graphicLoader
.graphics
.lineStyle
(1, 0xffffff, 1);
121 var center_x
:Number = stage
.stageWidth
/ 2;
122 var center_y
:Number = stage
.stageHeight
/ 2;
123 graphicLoader
.graphics
.moveTo
(center_x
, center_y
);
124 var fp
:Boolean = false;
125 for (var i
:Number = 0; i
>= c
; i
--)
127 x
= Math.sin
(Math.PI
/ 90 * i
) * 100 + center_x
;
128 y
= Math.cos
(Math.PI
/ 90 * i
) * 100 + center_y
;
129 graphicLoader
.graphics
.lineTo
(x
, y
);
132 graphicLoader
.filters
= new Array(glow
);
135 private function handleComplete
(event
:Event):void
140 private function handleInitProgress
(event
:Event):void
144 private function ioError
(e
:IOErrorEvent):void
149 private function progress
(e
:ProgressEvent):void
153 private function loadingFinished
():void
155 //removeEventListener(Event.ENTER_FRAME, checkFrame);
156 loaderInfo
.removeEventListener
(ProgressEvent.PROGRESS
, progress
);
157 loaderInfo
.removeEventListener
(IOErrorEvent.IO_ERROR
, ioError
);
159 removeChild
(txtLoading
);
164 private function startup
():void
166 var mainClass
:Class = getDefinitionByName
("Main") as Class;
167 addChild
(new mainClass
() as DisplayObject);
168 } // Implement IPreloaderDisplay interface
170 public function get backgroundColor
():uint
175 public function set backgroundColor
(value
:uint):void
179 public function get backgroundAlpha
():Number
184 public function set backgroundAlpha
(value
:Number):void
188 public function get backgroundImage
():Object
193 public function set backgroundImage
(value
:Object):void
197 public function get backgroundSize
():String
202 public function set backgroundSize
(value
:String):void
206 public function get stageWidth
():Number
211 public function set stageWidth
(value
:Number):void
215 public function get stageHeight
():Number
220 public function set stageHeight
(value
:Number):void