Load And Drag Image


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



Copy this code and paste it in your HTML
  1. package {
  2. import flash.display.Sprite;
  3. import flash.display.StageAlign;
  4. import flash.display.StageScaleMode;
  5.  
  6. import gs.plugins.SetSizePlugin;
  7. import gs.plugins.TweenPlugin;
  8.  
  9. public class Main extends Sprite
  10. {
  11. private var m_body:Sprite;
  12.  
  13. public function Main()
  14. {
  15. stage.scaleMode = StageScaleMode.NO_SCALE;
  16. stage.align = StageAlign.TOP_LEFT;
  17.  
  18. TweenPlugin.activate([SetSizePlugin]);
  19.  
  20. var image:LoadedImage = new LoadedImage("http://farm1.static.flickr.com/12/14705113_15f2172f03.jpg");
  21.  
  22. addChild(image);
  23. }
  24.  
  25. }
  26. }
  27.  
  28. import flash.display.Sprite;
  29. import flash.display.Loader;
  30. import flash.events.Event;
  31. import flash.display.Bitmap;
  32. import de.popforge.audio.processor.fl909.voices.Voice;
  33. import flash.net.URLRequest;
  34. import flash.events.MouseEvent;
  35. import gs.TweenLite;
  36. internal class LoadedImage extends Sprite
  37. {
  38. private var m_loader:Loader
  39. private var m_bitmap:Bitmap;
  40. private var m_overlay:Sprite;
  41. private var m_mask:Sprite;
  42. private var m_source:String;
  43.  
  44. public function LoadedImage(a_url:String)
  45. {
  46. m_source = a_url;
  47. initialize();
  48. load();
  49. }
  50.  
  51. private function initialize():void
  52. {
  53. m_loader = new Loader();
  54. m_overlay = new Sprite();
  55. m_mask = new Sprite();
  56.  
  57. addChild(m_mask);
  58. addChild(m_overlay);
  59.  
  60. mask = m_mask;
  61. mouseChildren = false;
  62. buttonMode = true;
  63.  
  64. m_overlay.alpha = 0;
  65.  
  66. initializeListeners();
  67. }
  68.  
  69. private function initializeListeners():void
  70. {
  71. addEventListener(MouseEvent.ROLL_OVER,mouseOverHandler,false,0,true);
  72. addEventListener(MouseEvent.ROLL_OUT,mouseOutHandler,false,0,true);
  73. addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler,false,0,true);
  74. }
  75.  
  76. private function mouseOverHandler(a_event:MouseEvent):void
  77. {
  78. TweenLite.to( m_overlay,0.15,{alpha:1});
  79. }
  80.  
  81. private function mouseOutHandler(a_event:MouseEvent):void
  82. {
  83. TweenLite.to( m_overlay,0.15,{alpha:0});
  84.  
  85. }
  86.  
  87. private function mouseDownHandler(a_event:MouseEvent):void
  88. {
  89. stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler,false,0,true);
  90. startDrag();
  91. }
  92.  
  93. private function mouseUpHandler(a_event:MouseEvent):void
  94. {
  95. stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
  96. stopDrag();
  97. }
  98.  
  99. private function load():void
  100. {
  101. m_loader.load( new URLRequest(m_source) );
  102. addLoaderListners();
  103. }
  104.  
  105. private function draw():void
  106. {
  107. if(!m_bitmap) return;
  108. addChildAt(m_bitmap,0);
  109.  
  110. with(m_overlay.graphics)
  111. {
  112. clear();
  113. beginFill(0xFF0000,0.5);
  114. drawRect(0,0,m_bitmap.width,m_bitmap.height);
  115. endFill();
  116. }
  117.  
  118. with(m_mask.graphics)
  119. {
  120. clear();
  121. beginFill(0x00FF00,1);
  122. drawRect(0,m_bitmap.height*.25,m_bitmap.width,m_bitmap.height*.5);
  123. endFill();
  124. }
  125.  
  126. }
  127.  
  128. private function addLoaderListners():void
  129. {
  130. m_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
  131. }
  132.  
  133. private function removeLoaderListners():void
  134. {
  135. m_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loaderCompleteHandler);
  136. }
  137.  
  138. private function loaderCompleteHandler(a_event:Event):void
  139. {
  140. removeLoaderListners();
  141. m_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loaderCompleteHandler);
  142. m_bitmap = m_loader.content as Bitmap;
  143. draw();
  144.  
  145. }
  146. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.