PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/org/flixel/system/FlxPreloader.as

http://github.com/AdamAtomic/flixel
ActionScript | 293 lines | 225 code | 16 blank | 52 comment | 27 complexity | fcc8d18212060916c1f68703dcfeb25a MD5 | raw file
  1. package org.flixel.system
  2. {
  3. import flash.display.Bitmap;
  4. import flash.display.BitmapData;
  5. import flash.display.DisplayObject;
  6. import flash.display.MovieClip;
  7. import flash.display.Sprite;
  8. import flash.display.StageAlign;
  9. import flash.display.StageScaleMode;
  10. import flash.events.Event;
  11. import flash.events.MouseEvent;
  12. import flash.net.URLRequest;
  13. import flash.net.navigateToURL;
  14. import flash.text.TextField;
  15. import flash.text.TextFormat;
  16. import flash.utils.getDefinitionByName;
  17. import flash.utils.getTimer;
  18. import org.flixel.FlxG;
  19. /**
  20. * This class handles the 8-bit style preloader.
  21. */
  22. public class FlxPreloader extends MovieClip
  23. {
  24. [Embed(source="../data/logo.png")] protected var ImgLogo:Class;
  25. [Embed(source="../data/logo_corners.png")] protected var ImgLogoCorners:Class;
  26. [Embed(source="../data/logo_light.png")] protected var ImgLogoLight:Class;
  27. /**
  28. * @private
  29. */
  30. protected var _init:Boolean;
  31. /**
  32. * @private
  33. */
  34. protected var _buffer:Sprite;
  35. /**
  36. * @private
  37. */
  38. protected var _bmpBar:Bitmap;
  39. /**
  40. * @private
  41. */
  42. protected var _text:TextField;
  43. /**
  44. * Useful for storing "real" stage width if you're scaling your preloader graphics.
  45. */
  46. protected var _width:uint;
  47. /**
  48. * Useful for storing "real" stage height if you're scaling your preloader graphics.
  49. */
  50. protected var _height:uint;
  51. /**
  52. * @private
  53. */
  54. protected var _logo:Bitmap;
  55. /**
  56. * @private
  57. */
  58. protected var _logoGlow:Bitmap;
  59. /**
  60. * @private
  61. */
  62. protected var _min:uint;
  63. /**
  64. * This should always be the name of your main project/document class (e.g. GravityHook).
  65. */
  66. public var className:String;
  67. /**
  68. * Set this to your game's URL to use built-in site-locking.
  69. */
  70. public var myURL:String;
  71. /**
  72. * Change this if you want the flixel logo to show for more or less time. Default value is 0 seconds.
  73. */
  74. public var minDisplayTime:Number;
  75. /**
  76. * Constructor
  77. */
  78. public function FlxPreloader()
  79. {
  80. minDisplayTime = 0;
  81. stop();
  82. stage.scaleMode = StageScaleMode.NO_SCALE;
  83. stage.align = StageAlign.TOP_LEFT;
  84. //Check if we are on debug or release mode and set _DEBUG accordingly
  85. try
  86. {
  87. throw new Error("Setting global debug flag...");
  88. }
  89. catch(E:Error)
  90. {
  91. var re:RegExp = /\[.*:[0-9]+\]/;
  92. FlxG.debug = re.test(E.getStackTrace());
  93. }
  94. var tmp:Bitmap;
  95. if(!FlxG.debug && (myURL != null) && (root.loaderInfo.url.indexOf(myURL) < 0))
  96. {
  97. tmp = new Bitmap(new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFFFFFFFF));
  98. addChild(tmp);
  99. var format:TextFormat = new TextFormat();
  100. format.color = 0x000000;
  101. format.size = 16;
  102. format.align = "center";
  103. format.bold = true;
  104. format.font = "system";
  105. var textField:TextField = new TextField();
  106. textField.width = tmp.width-16;
  107. textField.height = tmp.height-16;
  108. textField.y = 8;
  109. textField.multiline = true;
  110. textField.wordWrap = true;
  111. textField.embedFonts = true;
  112. textField.defaultTextFormat = format;
  113. textField.text = "Hi there! It looks like somebody copied this game without my permission. Just click anywhere, or copy-paste this URL into your browser.\n\n"+myURL+"\n\nto play the game at my site. Thanks, and have fun!";
  114. addChild(textField);
  115. textField.addEventListener(MouseEvent.CLICK,goToMyURL);
  116. tmp.addEventListener(MouseEvent.CLICK,goToMyURL);
  117. return;
  118. }
  119. this._init = false;
  120. addEventListener(Event.ENTER_FRAME, onEnterFrame);
  121. }
  122. private function goToMyURL(event:MouseEvent=null):void
  123. {
  124. navigateToURL(new URLRequest("http://"+myURL));
  125. }
  126. private function onEnterFrame(event:Event):void
  127. {
  128. if(!this._init)
  129. {
  130. if((stage.stageWidth <= 0) || (stage.stageHeight <= 0))
  131. return;
  132. create();
  133. this._init = true;
  134. }
  135. graphics.clear();
  136. var time:uint = getTimer();
  137. if((framesLoaded >= totalFrames) && (time > _min))
  138. {
  139. removeEventListener(Event.ENTER_FRAME, onEnterFrame);
  140. nextFrame();
  141. var mainClass:Class = Class(getDefinitionByName(className));
  142. if(mainClass)
  143. {
  144. var app:Object = new mainClass();
  145. addChild(app as DisplayObject);
  146. }
  147. destroy();
  148. }
  149. else
  150. {
  151. var percent:Number = root.loaderInfo.bytesLoaded/root.loaderInfo.bytesTotal;
  152. if((_min > 0) && (percent > time/_min))
  153. percent = time/_min;
  154. update(percent);
  155. }
  156. }
  157. /**
  158. * Override this to create your own preloader objects.
  159. * Highly recommended you also override update()!
  160. */
  161. protected function create():void
  162. {
  163. _min = 0;
  164. if(!FlxG.debug)
  165. _min = minDisplayTime*1000;
  166. _buffer = new Sprite();
  167. _buffer.scaleX = 2;
  168. _buffer.scaleY = 2;
  169. addChild(_buffer);
  170. _width = stage.stageWidth/_buffer.scaleX;
  171. _height = stage.stageHeight/_buffer.scaleY;
  172. _buffer.addChild(new Bitmap(new BitmapData(_width,_height,false,0x00345e)));
  173. var bitmap:Bitmap = new ImgLogoLight();
  174. bitmap.smoothing = true;
  175. bitmap.width = bitmap.height = _height;
  176. bitmap.x = (_width-bitmap.width)/2;
  177. _buffer.addChild(bitmap);
  178. _bmpBar = new Bitmap(new BitmapData(1,7,false,0x5f6aff));
  179. _bmpBar.x = 4;
  180. _bmpBar.y = _height-11;
  181. _buffer.addChild(_bmpBar);
  182. _text = new TextField();
  183. _text.defaultTextFormat = new TextFormat("system",8,0x5f6aff);
  184. _text.embedFonts = true;
  185. _text.selectable = false;
  186. _text.multiline = false;
  187. _text.x = 2;
  188. _text.y = _bmpBar.y - 11;
  189. _text.width = 80;
  190. _buffer.addChild(_text);
  191. _logo = new ImgLogo();
  192. _logo.scaleX = _logo.scaleY = _height/8;
  193. _logo.x = (_width-_logo.width)/2;
  194. _logo.y = (_height-_logo.height)/2;
  195. _buffer.addChild(_logo);
  196. _logoGlow = new ImgLogo();
  197. _logoGlow.smoothing = true;
  198. _logoGlow.blendMode = "screen";
  199. _logoGlow.scaleX = _logoGlow.scaleY = _height/8;
  200. _logoGlow.x = (_width-_logoGlow.width)/2;
  201. _logoGlow.y = (_height-_logoGlow.height)/2;
  202. _buffer.addChild(_logoGlow);
  203. bitmap = new ImgLogoCorners();
  204. bitmap.smoothing = true;
  205. bitmap.width = _width;
  206. bitmap.height = _height;
  207. _buffer.addChild(bitmap);
  208. bitmap = new Bitmap(new BitmapData(_width,_height,false,0xffffff));
  209. var i:uint = 0;
  210. var j:uint = 0;
  211. while(i < _height)
  212. {
  213. j = 0;
  214. while(j < _width)
  215. bitmap.bitmapData.setPixel(j++,i,0);
  216. i+=2;
  217. }
  218. bitmap.blendMode = "overlay";
  219. bitmap.alpha = 0.25;
  220. _buffer.addChild(bitmap);
  221. }
  222. protected function destroy():void
  223. {
  224. removeChild(_buffer);
  225. _buffer = null;
  226. _bmpBar = null;
  227. _text = null;
  228. _logo = null;
  229. _logoGlow = null;
  230. }
  231. /**
  232. * Override this function to manually update the preloader.
  233. *
  234. * @param Percent How much of the program has loaded.
  235. */
  236. protected function update(Percent:Number):void
  237. {
  238. _bmpBar.scaleX = Percent*(_width-8);
  239. _text.text = "FLX v"+FlxG.LIBRARY_MAJOR_VERSION+"."+FlxG.LIBRARY_MINOR_VERSION+" "+Math.floor(Percent*100)+"%";
  240. _text.setTextFormat(_text.defaultTextFormat);
  241. if(Percent < 0.1)
  242. {
  243. _logoGlow.alpha = 0;
  244. _logo.alpha = 0;
  245. }
  246. else if(Percent < 0.15)
  247. {
  248. _logoGlow.alpha = Math.random();
  249. _logo.alpha = 0;
  250. }
  251. else if(Percent < 0.2)
  252. {
  253. _logoGlow.alpha = 0;
  254. _logo.alpha = 0;
  255. }
  256. else if(Percent < 0.25)
  257. {
  258. _logoGlow.alpha = 0;
  259. _logo.alpha = Math.random();
  260. }
  261. else if(Percent < 0.7)
  262. {
  263. _logoGlow.alpha = (Percent-0.45)/0.45;
  264. _logo.alpha = 1;
  265. }
  266. else if((Percent > 0.8) && (Percent < 0.9))
  267. {
  268. _logoGlow.alpha = 1-(Percent-0.8)/0.1;
  269. _logo.alpha = 0;
  270. }
  271. else if(Percent > 0.9)
  272. {
  273. _buffer.alpha = 1-(Percent-0.9)/0.1;
  274. }
  275. }
  276. }
  277. }