Posted By

chrisaiv on 10/23/08


Tagged

error loader as3 overlay EventADDED


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

THEPWN3R
tspitzr


AS3: Placing an Overlay Over an Image


 / Published in: ActionScript 3
 

So here's the scenario, suppose your loading an image, swf, etc but if any other kind of error occurs in your application, you want an error message to display above the image. There are many different way to accomplish this task so I thought it would be helpful to show a more unconventional technique using Event.Added

  1. var errorMc:MovieClip;
  2.  
  3. var assetLoader:Loader = new Loader();
  4. assetLoader.name = "assetLoader";
  5. assetLoader.contentLoaderInfo.addEventListener( Event.ACTIVATE, httpStatusHandler, false, 0, true );
  6. assetLoader.contentLoaderInfo.addEventListener( Event.COMPLETE, loaderCompleteHandler, false, 0, true );
  7. assetLoader.load( new URLRequest("http://farm3.static.flickr.com/2068/2226178289_3f9556c08f_b.jpg") );
  8.  
  9. var appStage:Stage = stage;
  10. appStage.addEventListener( Event.ADDED, addedHandler, false, 0, true );
  11. appStage.addEventListener( Event.REMOVED, removedHandler, false, 0, true );
  12.  
  13. function displayMessage( notice:* ):void
  14. {
  15. errorMc = new MovieClip();
  16. errorMc.mouseChildren = false;
  17. errorMc.name = "messageMc";
  18. errorMc.graphics.beginFill(0x000000, 0.5);
  19. errorMc.graphics.drawRect( 0, 0, appStage.stageWidth, appStage.stageHeight );
  20. errorMc.graphics.endFill();
  21.  
  22. var tf:TextFormat = new TextFormat();
  23. tf.font = "Verdana";
  24. tf.size = 16;
  25. tf.color = 0xffffff;
  26. tf.rightMargin = 10;
  27. tf.leftMargin = 10;
  28.  
  29. var displayTxt:TextField = new TextField();
  30. displayTxt.width = errorMc.width;
  31. displayTxt.height = errorMc.height;
  32. displayTxt.wordWrap = true;
  33. displayTxt.selectable = false;
  34. displayTxt.text = notice.toString();
  35. displayTxt.setTextFormat( tf );
  36.  
  37. errorMc.addChild( displayTxt );
  38. appStage.addChild( errorMc );
  39. }
  40.  
  41. function httpStatusHandler( e:* ):void
  42. {
  43. trace( e );
  44. displayMessage("httpStatusHandler:" + e );
  45. }
  46.  
  47. function centerBitmap( bmp:Bitmap ):void
  48. {
  49. var centerPoint:Point = new Point( appStage.stageWidth / 2, appStage.stageHeight / 2 );
  50.  
  51. bmp.x = centerPoint.x - ( bmp.width / 2 );
  52. bmp.y = centerPoint.y - ( bmp.height / 2 );
  53. }
  54.  
  55. function getAspectRatio( width:Number, height:Number ):Number
  56. {
  57. if (width > height)
  58. return ( width / height );
  59. else
  60. return ( height / width );
  61. }
  62.  
  63. function adjustImageSize( bmp:Bitmap ):void
  64. {
  65. var aspectRatio:Number = getAspectRatio( bmp.width, bmp.height );
  66.  
  67. if( bmp.height > bmp.width && bmp.height > appStage.stageHeight ){
  68. bmp.height = appStage.stageHeight;
  69. bmp.width = bmp.height / aspectRatio;
  70. } else if ( bmp.width > appStage.stageWidth ) {
  71. bmp.width = appStage.stageWidth;
  72. bmp.height = bmp.width / aspectRatio;
  73. }
  74. centerBitmap( bmp );
  75. }
  76.  
  77. function loaderCompleteHandler( e:Event ):void
  78. {
  79. var bmp:Bitmap = e.currentTarget.content
  80. adjustImageSize( bmp );
  81. appStage.addChild( e.currentTarget.loader );
  82. }
  83.  
  84. function addedHandler( e:Event ):void
  85. {
  86. if ( e.target.name == "assetLoader") {
  87. if ( appStage.getChildByName("messageMc") != null) {
  88. var error:DisplayObject = appStage.getChildByName("messageMc");
  89. if ( appStage.contains( error ) ) {
  90. appStage.swapChildren( appStage.getChildByName( e.target.name ) , error );
  91. }
  92. }
  93. }
  94. }
  95.  
  96. function removedHandler( e:Event ):void {
  97. trace( "removedHandler: " + e.target.name );
  98. }

Report this snippet  

You need to login to post a comment.