AS3: Using Loader for SWFs, JPEGs, GIF, and PNGs


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

The Loader class is used for handling Bitmap data such as jpegs, pngs and gifs. The only handlers most will find relevant are progressHandler and onCompleteHandler but I listed all of the available ones for people interested in creating bulletproof apps.


Copy this code and paste it in your HTML
  1. /********************************
  2. Event Listeners
  3. ********************************/
  4. var imgLoader:Loader = new Loader();
  5. initBasicListeners( imgLoader );
  6. imgLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler, false, 0, true);
  7. imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler, false, 0, true);
  8. imgLoader.load(new URLRequest(asset));
  9.  
  10. //These Event Listeners are used a lot so let's try to minimize redundancies
  11. function initBasicListeners(dispatcher:IEventDispatcher):void
  12. {
  13. dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler, false, 0, true);
  14. dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler, false, 0, true);
  15. dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler, false, 0, true);
  16. }
  17.  
  18. /********************************
  19. Event Handlers
  20. ********************************/
  21. function httpStatusHandler (e:Event):void
  22. {
  23. //trace("httpStatusHandler:" + e);
  24. }
  25. function securityErrorHandler (e:Event):void
  26. {
  27. trace("securityErrorHandler:" + e);
  28. }
  29. function ioErrorHandler(e:Event):void
  30. {
  31. trace("ioErrorHandler: " + e);
  32. }
  33. function progressHandler(e:Event):void
  34. {
  35. trace(e.currentTarget.bytesLoaded + " / " + e.currentTarget.bytesTotal);
  36. }
  37.  
  38. function onCompleteHandler (e:Event):void
  39. {
  40. trace("imgCompleteHandler:" + e.currentTarget.content + " " + e.currentTarget.loader);
  41. addChild( e.currentTarget.loader );
  42. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.