Posted By

jdsantiagojr on 02/04/11


Tagged

actionscript flash as3


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

merritt212
makeyko


AS3 Preloader


 / Published in: ActionScript 3
 

  1. package
  2. {
  3. import fl.controls.Label;
  4. import fl.controls.ProgressBar;
  5. import fl.controls.ProgressBarDirection;
  6. import fl.controls.ProgressBarMode;
  7. import flash.display.DisplayObject;
  8. import flash.display.MovieClip;
  9. import flash.display.Sprite;
  10. import flash.display.StageAlign;
  11. import flash.display.StageScaleMode;
  12. import flash.events.Event;
  13. import flash.events.IOErrorEvent;
  14. import flash.events.ProgressEvent;
  15. import flash.utils.getDefinitionByName;
  16.  
  17. /**
  18. * ...
  19. * @author John
  20. */
  21. public class Preloader extends MovieClip
  22. {
  23. private var _loader:Sprite;
  24. private var _progressText:Label;
  25. private var _progressBar:ProgressBar;
  26.  
  27. public function Preloader()
  28. {
  29. if (stage) {
  30. stage.scaleMode = StageScaleMode.NO_SCALE;
  31. stage.align = StageAlign.TOP_LEFT;
  32. }
  33. addEventListener(Event.ENTER_FRAME, checkFrame);
  34. loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
  35. loaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioError);
  36.  
  37. // show loader
  38. showLoader();
  39. }
  40.  
  41. private function ioError(e:IOErrorEvent):void
  42. {
  43. trace(e.text);
  44. }
  45.  
  46. private function progress(e:ProgressEvent):void
  47. {
  48. // update loader
  49. var bytesLoaded:int = loaderInfo.bytesLoaded;
  50. var bytesTotal:int = loaderInfo.bytesTotal;
  51.  
  52. var kiloBytesLoaded:int = bytesLoaded / 1024;
  53. var kiloBytesTotal:int = bytesTotal / 1024;
  54.  
  55. _progressBar.setProgress(bytesLoaded, bytesTotal);
  56. _progressText.text = kiloBytesLoaded + "K / " + kiloBytesTotal + "K";
  57. }
  58.  
  59. private function checkFrame(e:Event):void
  60. {
  61. if (loaderInfo.bytesLoaded == loaderInfo.bytesTotal)
  62. {
  63. stop();
  64. loadingFinished();
  65. }
  66. }
  67.  
  68. private function loadingFinished():void
  69. {
  70. removeEventListener(Event.ENTER_FRAME, checkFrame);
  71. loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
  72. loaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, ioError);
  73.  
  74. // hide loader
  75. removeChild(_loader);
  76. startup();
  77. }
  78.  
  79. private function startup():void
  80. {
  81. var mainClass:Class = getDefinitionByName("Main") as Class;
  82. if (parent == stage) stage.addChildAt(new mainClass() as DisplayObject, 0);
  83. else addChildAt(new mainClass() as DisplayObject, 0);
  84. }
  85.  
  86. private function showLoader():void
  87. {
  88. _loader = new Sprite();
  89.  
  90. _progressBar = new ProgressBar();
  91. _progressBar.direction = ProgressBarDirection.RIGHT;
  92. _progressBar.mode = ProgressBarMode.MANUAL;
  93. _progressBar.move(10, 10);
  94. _loader.addChild(_progressBar);
  95.  
  96. _progressText = new Label();
  97. _progressText.move(10, 12);
  98. _loader.addChild(_progressText);
  99.  
  100. addChild(_loader);
  101. }
  102.  
  103. }
  104.  
  105. }

Report this snippet  

You need to login to post a comment.