AS3: preloader code


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



Copy this code and paste it in your HTML
  1. var myRequest:URLRequest = new URLRequest("/someDir/someFile");
  2. var myLoader:Loader = new Loader();
  3.  
  4. myLoader.load(myRequest);
  5.  
  6. myLoader.contentLoaderInfo.addEventListener(Event.OPEN,showPreloader);
  7. myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,showProgress);
  8. myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,showContent);
  9.  
  10. var myPreloader:Preloader = new Preloader();
  11.  
  12. function showPreloader(event:Event):void {
  13. addChild(myPreloader);
  14. myPreloader.x = stage.stageWidth/2;
  15. myPreloader.y = stage.stageHeight/2;
  16. }
  17.  
  18. function showProgress(event:ProgressEvent):void {
  19. var percentLoaded:Number = event.bytesLoaded/event.bytesTotal;
  20. myPreloader.loading_txt.text = "Loading - " + Math.round(percentLoaded * 100) + "%";
  21. myPreloader.bar_mc.width = 198 * percentLoaded;
  22. }
  23.  
  24. function showContent(event:Event):void {
  25. removeChild(myPreloader);
  26. addChild(myLoader);
  27. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.