/
Preloader.as
138 lines (101 loc) · 2.98 KB
/
Preloader.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package
{
import flash.display.*;
import flash.text.*;
import flash.events.*;
import flash.utils.getDefinitionByName;
[SWF(width = "640", height = "480")]
public class Preloader extends Sprite
{
// Change these values
private static const mustClick: Boolean = false;
private static const mainClassName: String = "Main";
private static const BG_COLOR:uint = 0x000000;
private static const FG_COLOR:uint = 0xFFFFFF;
// Ignore everything else
private var progressBar: Shape;
private var text: TextField;
private var px:int;
private var py:int;
private var w:int;
private var h:int;
private var sw:int;
private var sh:int;
[Embed(source = 'net/flashpunk/graphics/04B_03__.TTF', fontFamily = 'default')]
private static const FONT:Class;
public function Preloader ()
{
sw = stage.stageWidth;
sh = stage.stageHeight;
w = stage.stageWidth * 0.8;
h = 20;
px = (sw - w) * 0.5;
py = (sh - h) * 0.5;
graphics.beginFill(BG_COLOR);
graphics.drawRect(0, 0, sw, sh);
graphics.endFill();
graphics.beginFill(FG_COLOR);
graphics.drawRect(px - 2, py - 2, w + 4, h + 4);
graphics.endFill();
progressBar = new Shape();
addChild(progressBar);
text = new TextField();
text.textColor = FG_COLOR;
text.selectable = false;
text.mouseEnabled = false;
text.defaultTextFormat = new TextFormat("default", 16);
text.embedFonts = true;
text.autoSize = "left";
text.text = "0%";
text.x = (sw - text.width) * 0.5;
text.y = sh * 0.5 + h;
addChild(text);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
if (mustClick) {
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
}
public function onEnterFrame (e:Event): void
{
if (hasLoaded())
{
graphics.clear();
graphics.beginFill(BG_COLOR);
graphics.drawRect(0, 0, sw, sh);
graphics.endFill();
if (! mustClick) {
startup();
} else {
text.scaleX = 2.0;
text.scaleY = 2.0;
text.text = "Click to start";
text.y = (sh - text.height) * 0.5;
}
} else {
var p:Number = (loaderInfo.bytesLoaded / loaderInfo.bytesTotal);
progressBar.graphics.clear();
progressBar.graphics.beginFill(BG_COLOR);
progressBar.graphics.drawRect(px, py, p * w, h);
progressBar.graphics.endFill();
text.text = int(p * 100) + "%";
}
text.x = (sw - text.width) * 0.5;
}
private function onMouseDown(e:MouseEvent):void {
if (hasLoaded())
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
startup();
}
}
private function hasLoaded (): Boolean {
return (loaderInfo.bytesLoaded >= loaderInfo.bytesTotal);
}
private function startup (): void {
stage.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
var mainClass:Class = getDefinitionByName(mainClassName) as Class;
parent.addChild(new mainClass as DisplayObject);
parent.removeChild(this);
}
}
}