先看图,按下鼠标拖动。
主要代码如下:
package { import com.duowan.util.DrawUtil; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.geom.Point; /** * ... * @author hacker47 */ public class Main extends Sprite { private var p0:Point = new Point(); private var drawWidth:Number; private var drawHeight:Number; public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point 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 { var dx:Number = mouseX - p0.x; var dy:Number = mouseY -p0.y; graphics.clear(); DrawUtil.drawRect(graphics, p0, dx, dy); } private function onUp(e:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove); } } }
public static function drawDashed(graphics:Graphics,p1:Point,p2:Point,length:Number=5,gap:Number=5):void { graphics.lineStyle(1); var max:Number = Point.distance(p1,p2); var l:Number = 0; var p3:Point; var p4:Point; while(lmax)l=max p4 = Point.interpolate(p2,p1,l/max); graphics.moveTo(p3.x,p3.y) graphics.lineTo(p4.x,p4.y) l+=gap; } }