Common class for loading and preloading an external flash or image


/ Published in: ActionScript 3
Save to your folder(s)

See also: http://code.google.com/p/undolibrary/


Copy this code and paste it in your HTML
  1. /**
  2. * Common class for loading an external flash or image
  3. *
  4. * @class UndoLoader
  5. * @author Giovambattista Fazioli <[email protected]>
  6. * @web http://www.undolog.com
  7. * @version 1.0
  8. *
  9. */
  10. import flash.display.*;
  11. import flash.events.*;
  12. import flash.text.*;
  13. import flash.net.*;
  14. import flash.system.*;
  15. //
  16. public class UndoLoader extends MovieClip {
  17. private var _preloader:Preloader;
  18. private var _loader:Loader;
  19. private var _bar:MovieClip;
  20. private var _perc:TextField;
  21.  
  22. /**
  23. * Class constructor
  24. *
  25. * @param void
  26. * @return void
  27. */
  28. public function UndoLoader() {
  29. stage.scaleMode = StageScaleMode.NO_SCALE
  30. stage.align = StageAlign.TOP_LEFT;
  31. addEventListener( Event.ADDED_TO_STAGE, init );
  32. }
  33.  
  34. /**
  35. * Init bar and content. Trigged when added to stage
  36. *
  37. * @param {event} e Passed from addEventListener(). Can be null
  38. * @return void
  39. * @private
  40. */
  41. private function init(e:Event = null):void {
  42. initBar();
  43. loadContent();
  44. }
  45.  
  46. /**
  47. * Create the preloader movie and init it.
  48. *
  49. * @param void
  50. * @return void
  51. * @private
  52. */
  53. private function initBar():void {
  54. _preloader = new Preloader();
  55. _bar = _preloader.bar;
  56. _bar.scaleX = 0;
  57. _perc = _preloader.perc;
  58. _perc.text = '0%';
  59. _preloader.x = (stage.stageWidth - _preloader.width)/2;
  60. _preloader.y = (stage.stageHeight - _preloader.height)/2;
  61. addChild( _preloader );
  62. }
  63.  
  64. /**
  65. * Creare a Loader() object and init PROGRESS and COMPLETE event
  66. *
  67. * @param void
  68. * @return void
  69. * @private
  70. */
  71. private function loadContent():void {
  72. _loader = new Loader();
  73. _loader.contentLoaderInfo.addEventListener( ProgressEvent.PROGRESS, inProgress );
  74. _loader.contentLoaderInfo.addEventListener( Event.COMPLETE, onComplete );
  75. _loader.load( new URLRequest( this.loaderInfo.parameters.filename ) );
  76. addChild( _loader );
  77. }
  78.  
  79. /**
  80. * Trigged when loading in progress. Move the progress bar and set
  81. * the text string
  82. *
  83. * @param {ProgressEvent} e ProgressEvent
  84. * @return void
  85. * @private
  86. */
  87. private function inProgress(e:ProgressEvent = null):void {
  88. var p:Number = Math.floor( (e.bytesLoaded / e.bytesTotal)*100 );
  89. _perc.text = p + '%';
  90. _bar.scaleX = p/100;
  91. }
  92.  
  93. /**
  94. * Trigged when loading is finished. Remove the preloader movie
  95. * from the stage. Get the framerate from loaded movie and set
  96. * framerate to main movie.
  97. *
  98. * @param {Event} e Event
  99. * @return void
  100. * @private
  101. */
  102. private function onComplete(e:Event = null):void {
  103. stage.frameRate = _loader.contentLoaderInfo.frameRate;
  104. removeChild( _preloader );
  105. }
  106. }

URL: http://www.undolog.com/2008/06/16/creare-un-preloader-generico-in-actionscript-30/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.