Posted By

blackOffice on 04/11/10


Tagged

preloader


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

socialflasher
TrentSkunk
tspitzr


AS3 Preloader


 / Published in: ActionScript 3
 

Simple preloader than displays overall progress and bytes loaded. Main timeline and movie to be loaded must be passed through in order for it to work. eg. var preloader:preLoader = new preLoader(this, "myfile.swf");

  1. package
  2. {
  3. import flash.display.Loader;
  4. import flash.display.MovieClip;
  5. import flash.events.Event;
  6. import flash.events.ProgressEvent;
  7. import flash.net.URLRequest;
  8.  
  9. /**
  10. * ...
  11. * @author [email protected]
  12. */
  13.  
  14. public class preLoader extends MovieClip
  15. {
  16. private var timeline:MovieClip;
  17. private var mainLoader:Loader;
  18.  
  19. public function preLoader(mainTimeline:MovieClip, path:String) {
  20. trace("preloading");
  21. timeline = mainTimeline;
  22. mainLoader = new Loader();
  23. var swfPath:URLRequest = new URLRequest(path);
  24. mainLoader.load(swfPath);
  25. mainLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, getProgress);
  26. mainLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
  27. }
  28. private function getProgress(event:ProgressEvent) {
  29. var currentBytes:Number = event.bytesLoaded;
  30. var total:Number = event.bytesTotal;
  31. var progress:Number = Math.floor((currentBytes * 100) / total);
  32. trace("progress : " + progress);
  33. trace("total : " + total);
  34. timeline.loadInfo.text = currentBytes.toString() + "/" + total.toString() + " | " + progress.toString() + "%"; //loadInfo is a textBox on the main timeline
  35. }
  36. private function onComplete(event:Event) {
  37. var mainContent:MovieClip = MovieClip(event.target.content);
  38. trace("event.target.content : " + event.target.content);
  39. timeline.addChild(mainContent);
  40. timeline.removeChild(timeline.loadInfo);
  41. }
  42. }
  43.  
  44. }

Report this snippet  

You need to login to post a comment.