代码:
package { import com.duowan.util.DrawUtil; import flash.display.DisplayObject; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Point; import flash.geom.Rectangle; import flash.text.TextField; /** * ... * @author hacker47 */ public class SelectMain extends Sprite { private var p0:Point = new Point(); private var drawWidth:Number; private var drawHeight:Number; public function SelectMain() { stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown); } private function onDown(e:MouseEvent):void { p0.x=mouseX; p0.y= mouseY; stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove); stage.addEventListener(MouseEvent.MOUSE_UP, onUp); } private function onMove(e:MouseEvent):void { drawWidth = mouseX - p0.x; drawHeight = mouseY -p0.y; graphics.clear(); DrawUtil.drawRect(graphics, p0, drawWidth, drawHeight); } private function onUp(e:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove); checkRange(); } private function checkRange():void { if (drawWidth < 0) { drawWidth = -drawWidth; p0.x -= drawWidth; } if (drawHeight < 0) { drawHeight = -drawHeight; p0.y -= drawHeight; } for (var i:int = 0; i < this.numChildren; i++ ) { var d:DisplayObject = getChildAt(i); var rect:Rectangle = d.getRect(this); if (rect.x > p0.x && rect.x < (p0.x + drawWidth)) { if (rect.y > p0.y && rect.y < (p0.y + drawHeight)) { selectedDraw(rect); } } } } private function selectedDraw(rect:Rectangle):void { graphics.lineStyle(2, 0x336699); graphics.drawRect(rect.x, rect.y, rect.width, rect.height); } } }