flash绘图api实现点选线

package lib
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;

	public class PointSelectLine extends Sprite
	{
		private var shape:Sprite;
		private var color:uint;
		private var startPoint:Point;
		
		public function PointSelectLine()
		{
			addEventListener(Event.ADDED_TO_STAGE,init);
		}
		private function init(e:Event):void{
			stage.addEventListener(MouseEvent.MOUSE_UP,onUp);
			stage.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
		}
		private function onUp(e:MouseEvent):void{
			stage.removeEventListener(MouseEvent.MOUSE_MOVE,onMove);
		}
		private function onDown(e:MouseEvent):void{
			startPoint = new Point(stage.mouseX,stage.mouseY);
			color = Math.random() * 0xffffff;
			initShape();
			stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
		}
		private function onMove(e:MouseEvent):void{
			shape.graphics.clear();
			shape.graphics.lineStyle(3,color);
			shape.graphics.moveTo(startPoint.x,startPoint.y);
			shape.graphics.lineTo(stage.mouseX,stage.mouseY);
		}
		private function initShape():void{
			shape = new Sprite();
			addChild(shape);
			shape.addEventListener(MouseEvent.CLICK,onClick);
		}
		private function onClick(e:MouseEvent):void{
			if(e.target.filters.length==0){
				e.target.filters = [new GlowFilter()];
			}else{
				e.target.filters = [];
			}
		}
	}
}

你可能感兴趣的:(Flash,UP)