¡¤Äúµ±Ç°µÄλÖãºÊ×Ò³ > ¼¼Êõ½Ì³Ì > AS2ÓëAS3¼¼Êõ >

[AS3]as3·ÂdzµÄͼƬ°Ë¸ö·½ÏòµÄ²ÃÇпò¹¦ÄÜ´úÂë²Î¿¼

ʱ¼ä:2013-10-16 11:19dewen.org
Ô­Àí£ºÍ¼Æ¬²Ã¼ô¿ò£¬Êµ¼Ê¾ÍÊÇÒ»¸öShape»òÕßSprite£¬ÀïÃæ°üº¬ÁËËĸö»òÕ߰˸ö¿ØÖÆ·½¿é(¿ØÖÆÇø)£¬ÕâЩ¿ØÖÆÇø¸ù¾ÝÊó±êʼþÒƶ¯¸Ä±ä²Ã¼ôÇøµÄ´óС¡£

˵ÏÂÔ­Àí£º
ͼƬ²Ã¼ô¿ò£¬Êµ¼Ê¾ÍÊÇÒ»¸öShape»òÕßSprite£¬ÀïÃæ°üº¬ÁËËĸö»òÕ߰˸ö¿ØÖÆ·½¿é(¿ØÖÆÇø)£¬ÕâЩ¿ØÖÆÇø¸ù¾ÝÊó±êʼþÒƶ¯¸Ä±ä²Ã¼ôÇøµÄ´óС¡£

´ÓÒÔÇ°×öµÄÒ»¸öÏîÄ¿ÖÐÄóöÀ´µÄ£¬ÉÔ΢ÐÞ¸ÄÁËÏ£¬¿ÉÒÔÖ±½ÓʹÓãº

  1. addChild(new Clip());//Ö§³Ö°Ë¸ö·½ÏòÈÎÒâµ÷Õû 
  1. package  { 
  2. import flash.display.Bitmap; 
  3. import flash.display.BitmapData; 
  4. import flash.display.Sprite; 
  5. import flash.events.Event; 
  6. import flash.events.MouseEvent; 
  7.  
  8. public class Clip extends Sprite { 
  9. private var m_selectBox:Bitmap; 
  10. private var m_dotBmpd:BitmapData; 
  11. private var m_dotTopLeft:Sprite; 
  12. private var m_dotTop:Sprite; 
  13. private var m_dotTopRight:Sprite; 
  14. private var m_dotLeft:Sprite; 
  15. private var m_dotRight:Sprite; 
  16. private var m_dotBottomLeft:Sprite; 
  17. private var m_dotBottom:Sprite; 
  18. private var m_dotBottomRight:Sprite; 
  19. private var m_target:Sprite; 
  20.  
  21. private var m_dotWidth:uint; 
  22. private var m_dotHeight:uint; 
  23. private var m_oldMouseX:int; 
  24. private var m_oldMouseY:int; 
  25.  
  26. public function Clip() { 
  27. this.m_selectBox = new Bitmap(new BitmapData(10, 10, true, 0x20ff0000)); 
  28. this.m_dotBmpd = new BitmapData(5, 5, false, 0); 
  29. this.createSelection(); 
  30.  
  31. private function createSelection():void { 
  32. this.addChild(this.m_selectBox); 
  33. var bmd:BitmapData = this.m_dotBmpd; 
  34. this.m_dotWidth = bmd.width; 
  35. this.m_dotHeight = bmd.height; 
  36.  
  37. this.m_dotLeft = new Sprite(); 
  38. this.m_dotLeft.addChild(new Bitmap(bmd)); 
  39. this.m_dotRight = new Sprite(); 
  40. this.m_dotRight.addChild(new Bitmap(bmd)); 
  41. this.addChild(this.m_dotLeft); 
  42. this.addChild(this.m_dotRight); 
  43. this.m_dotLeft.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseEventHandler); 
  44. this.m_dotRight.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseEventHandler); 
  45.  
  46. this.m_dotTop = new Sprite(); 
  47. this.m_dotTop.addChild(new Bitmap(bmd)); 
  48. this.m_dotBottom = new Sprite(); 
  49. this.m_dotBottom.addChild(new Bitmap(bmd)); 
  50. this.addChild(this.m_dotTop); 
  51. this.addChild(this.m_dotBottom); 
  52. this.m_dotTop.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseEventHandler); 
  53. this.m_dotBottom.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseEventHandler); 
  54.  
  55. this.m_dotBottomLeft = new Sprite(); 
  56. this.m_dotBottomLeft.addChild(new Bitmap(bmd)); 
  57. this.m_dotBottomRight = new Sprite(); 
  58. this.m_dotBottomRight.addChild(new Bitmap(bmd)); 
  59. this.m_dotTopLeft = new Sprite(); 
  60. this.m_dotTopLeft.addChild(new Bitmap(bmd)); 
  61. this.m_dotTopRight = new Sprite(); 
  62. this.m_dotTopRight.addChild(new Bitmap(bmd)); 
  63. this.addChild(this.m_dotBottomLeft); 
  64. this.addChild(this.m_dotBottomRight); 
  65. this.addChild(this.m_dotTopLeft); 
  66. this.addChild(this.m_dotTopRight); 
  67. this.m_dotBottomLeft.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseEventHandler); 
  68. this.m_dotBottomRight.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseEventHandler); 
  69. this.m_dotTopLeft.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseEventHandler); 
  70. this.m_dotTopRight.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseEventHandler); 
  71.  
  72. this.m_selectBox.width = 100
  73. this.m_selectBox.height = 100
  74. this.updateDotPosition(); 
  75.  
  76. private function updateDotPosition():void { 
  77. var width:uint = this.m_selectBox.width; 
  78. var height:uint = this.m_selectBox.height; 
  79. var dotHalfWidth:uint = this.m_dotWidth >> 1; 
  80. var dotHalfHeight:uint = this.m_dotHeight >> 1; 
  81. this.m_dotLeft.x = -dotHalfWidth + this.m_selectBox.x; 
  82. this.m_dotRight.x = width - dotHalfWidth - 1 + this.m_selectBox.x; 
  83. thisthis.m_dotLeft.y = this.m_dotRight.y = (height >> 1) - dotHalfHeight + this.m_selectBox.y; 
  84. thisthis.m_dotTop.x = this.m_dotBottom.x = (width >> 1) - dotHalfWidth + this.m_selectBox.x; 
  85. this.m_dotTop.y = -dotHalfHeight + this.m_selectBox.y; 
  86. this.m_dotBottom.y = height - dotHalfHeight - 1 + this.m_selectBox.y; 
  87. thisthis.m_dotTopLeft.x = this.m_dotBottomLeft.x = -dotHalfWidth + this.m_selectBox.x; 
  88. thisthis.m_dotTopLeft.y = this.m_dotTopRight.y = -dotHalfHeight + this.m_selectBox.y; 
  89. thisthis.m_dotBottomLeft.y = this.m_dotBottomRight.y = height - dotHalfHeight - 1 + this.m_selectBox.y; 
  90. thisthis.m_dotTopRight.x = this.m_dotBottomRight.x = width - dotHalfWidth - 1 + this.m_selectBox.x; 
  91.  
  92. private function mouseEventHandler(evt:MouseEvent):void { 
  93. switch(evt.type) { 
  94. case MouseEvent.MOUSE_DOWN: 
  95. evt.stopImmediatePropagation(); 
  96. this.m_oldMouseX = stage.mouseX; 
  97. this.m_oldMouseY = stage.mouseY; 
  98. this.m_target = Sprite(evt.currentTarget); 
  99. stage.addEventListener(MouseEvent.MOUSE_MOVE, this.mouseEventHandler); 
  100. stage.addEventListener(MouseEvent.MOUSE_UP, this.mouseEventHandler); 
  101. break; 
  102. case MouseEvent.MOUSE_UP: 
  103. this.stopDrag(); 
  104. this.m_target = null
  105. stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.mouseEventHandler); 
  106. stage.removeEventListener(MouseEvent.MOUSE_UP, this.mouseEventHandler); 
  107. break; 
  108. case MouseEvent.MOUSE_MOVE: 
  109. var offsetX:int = stage.mouseX - this.m_oldMouseX; 
  110. var offsetY:int = stage.mouseY - this.m_oldMouseY; 
  111. var width:uint = this.m_selectBox.width; 
  112. var height:uint = this.m_selectBox.height; 
  113. switch(this.m_target) { 
  114. case this.m_dotTop: 
  115. this.changeHeightY(offsetY, height, this.m_dotBottom); 
  116. break; 
  117. case this.m_dotBottom: 
  118. this.changeHeight(offsetY, height, this.m_dotTop); 
  119. break; 
  120. case this.m_dotLeft: 
  121. this.changeWidthX(offsetX, width, this.m_dotRight); 
  122. break; 
  123. case this.m_dotRight: 
  124. this.changeWidth(offsetX, width, this.m_dotLeft); 
  125. break; 
  126. case this.m_dotTopLeft: 
  127. this.changeWidthX(offsetX, width, this.m_dotTopRight); 
  128. if (this.m_target == this.m_dotTopRight) { 
  129. this.changeHeightY(offsetY, height, this.m_dotBottomRight); 
  130. }else { 
  131. this.changeHeightY(offsetY, height, this.m_dotBottomLeft); 
  132. break; 
  133. case this.m_dotTopRight: 
  134. this.changeWidth(offsetX, width, this.m_dotTopLeft); 
  135. if (this.m_target == this.m_dotTopLeft) { 
  136. this.changeHeightY(offsetY, height, this.m_dotBottomLeft); 
  137. }else { 
  138. this.changeHeightY(offsetY, height, this.m_dotBottomRight); 
  139. break; 
  140. case this.m_dotBottomLeft: 
  141. this.changeWidthX(offsetX, width, this.m_dotBottomRight); 
  142. if (this.m_target == this.m_dotBottomRight) { 
  143. this.changeHeight(offsetY, height, this.m_dotTopRight); 
  144. }else { 
  145. this.changeHeight(offsetY, height, this.m_dotTopLeft); 
  146. break; 
  147. case this.m_dotBottomRight: 
  148. this.changeWidth(offsetX, width, this.m_dotBottomLeft); 
  149. if (this.m_target == this.m_dotBottomLeft) { 
  150. this.changeHeight(offsetY, height, this.m_dotTopLeft); 
  151. }else { 
  152. this.changeHeight(offsetY, height, this.m_dotTopRight); 
  153. break; 
  154. break; 
  155.  
  156. private function changeWidth(offset:int, width:uint, nextTarget:Sprite):void { 
  157. if (offset == 0) return; 
  158. offset = width + offset; 
  159. if (offset < 1) { 
  160. this.m_selectBox.x += offset; 
  161. this.m_selectBox.width = Math.abs(offset) + 1; 
  162. this.m_target = nextTarget
  163. }else { 
  164. this.m_selectBox.width = offset
  165. this.m_oldMouseX = stage.mouseX; 
  166. this.updateDotPosition(); 
  167. private function changeWidthX(offset:int, width:uint, nextTarget:Sprite):void { 
  168. if (offset == 0) return; 
  169. offset = width - offset; 
  170. if (offset < 1) { 
  171. this.m_selectBox.x += width - 1; 
  172. this.m_selectBox.width = Math.abs(offset) + 1; 
  173. this.m_target = nextTarget
  174. }else { 
  175. this.m_selectBox.width = offset
  176. this.m_selectBox.x += width - offset; 
  177. this.m_oldMouseX = stage.mouseX; 
  178. this.updateDotPosition(); 
  179. private function changeHeight(offset:int, height:uint, nextTarget:Sprite):void { 
  180. if (offset == 0) return; 
  181. offset = height + offset; 
  182. if (offset < 1) { 
  183. this.m_selectBox.y += offset; 
  184. this.m_selectBox.height = Math.abs(offset) + 1; 
  185. this.m_target = nextTarget
  186. }else { 
  187. this.m_selectBox.height = offset
  188. this.m_oldMouseY = stage.mouseY; 
  189. this.updateDotPosition(); 
  190. private function changeHeightY(offset:int, height:uint, nextTarget:Sprite):void { 
  191. if (offset == 0) return; 
  192. offset = height - offset; 
  193. if (offset < 1) { 
  194. this.m_selectBox.y += height - 1; 
  195. this.m_selectBox.height = Math.abs(offset) + 1; 
  196. this.m_target = nextTarget
  197. }else { 
  198. this.m_selectBox.height = offset
  199. this.m_selectBox.y += height - offset; 
  200. this.m_oldMouseY = stage.mouseY; 
  201. this.updateDotPosition(); 

 

ÈÈÃÅÎÄÕÂÍƼö

ÇëÉÔºò...

±£ÀûÍþÊÓÔÆƽ̨-ÇáËÉʵÏֵ㲥ֱ²¥ÊÓƵӦÓÃ

¿á²¥ÔÆÊý¾Ýͳ¼Æ·ÖÎö¿çƽ̨²¥·ÅÆ÷