public class DrawControl extends Sprite
?{
??//舞台
??private var _stage:Stage;
??//要画的色块
??private var _shape:Shape;
??//保存鼠标的前一个点
??private var _oldPoint:Point;
??//线的大小
??private var _lineSize:uint=20;
??//线的最大值和最小值
??private var _lineMax:uint=20;
??private var _lineMin:uint=8;
??//移动的速度(决定画的线的大小)
??private var _mouseSp:uint=10;
??//线条的颜色
??private var _lineColor:int=0xffffff;
??//线条数组
??private var shapeArray:Array=new Array();
??public function DrawControl(stage:Stage)
??{
??? //创建并附加模糊滤镜给自己
???var blur:BlurFilter=new BlurFilter();
???blur.blurX=8;
???blur.blurY=8;
???blur.quality=BitmapFilterQuality.LOW;
???this.filters=[blur];
???
???_stage=stage;
???_stage.addEventListener(MouseEvent.MOUSE_DOWN,whenMouseDown);
???_stage.addEventListener(MouseEvent.MOUSE_UP,whenMouseUp);
???_stage.addEventListener(KeyboardEvent.KEY_UP,whenKeyUp);
???//初始化线的大小
???_lineSize=_lineMax;
???
??}
??
??public function whenKeyUp(evt:KeyboardEvent):void{
???var key:String=String.fromCharCode(evt.charCode);
?? //如果按的是r键,那么就进行删除
???if(key=='r'){
????var lastShape:Shape=shapeArray.pop() as Shape;
????if(lastShape!=null){
?????? removeChild(lastShape);
?????? lastShape=null;
????}
???}
??}
??public function whenMouseUp(evt:MouseEvent):void{
???trace("Up");
???_shape.graphics.endFill();
???_stage.removeEventListener(MouseEvent.MOUSE_MOVE,whenMouseMove);
??}
??public? function whenMouseDown(evt:MouseEvent):void{
???
???//判断删除画的_shape
//???if(_shape){
//????removeChild(_shape);
//????_shape=null;
//???}
???this._shape=new Shape();
???shapeArray.push(_shape);
???addChild(_shape);
???_shape.graphics.lineTo(mouseX,mouseY);
???//开始画线
???_shape.graphics.lineStyle(_lineSize,_lineColor,1,false,"normal");
???_stage.addEventListener(MouseEvent.MOUSE_MOVE,whenMouseMove);
???_oldPoint=new Point(mouseX,mouseY);
??}
??
??public? function whenMouseMove(evt:MouseEvent):void{
??????? var newPoint:Point=new Point(mouseX,mouseY);
???? //计算两点之间的距离
???? var distance:Number=Point.distance(_oldPoint,newPoint);
???? _oldPoint=newPoint;
???? //根据两点之间的距离长短来判断要画线的大小
???? if(distance>=_mouseSp){
????? _lineSize-=2;
???? }else{
????? _lineSize+=2;
???? }
???? //判断不让线大小,超出最大值和最小值
???? if(_lineSize>_lineMax){
????? _lineSize=_lineMax;
???? }else if(_lineSize<_lineMin){
????? _lineSize=_lineMin;
???? }
???? _shape.graphics.lineStyle(_lineSize,"normal");
???? _shape.graphics.lineTo(mouseX,mouseY);
??}
?}
好,看下运行效果,写了个 你 ,然后我们在按r键删除画好的线
![](http://img50.lidatong.com.cn//uploads/allimg/c20201215/503fb6680da973cac09b4223a0dcc7b4.gif)
????????? ![](http://img50.lidatong.com.cn//uploads/allimg/c20201215/503fb6680da973cac09b4223a0dcc7b4.gif)
好的,基本上实现了划线和删除的功能,算是一个小涂鸦了
![](http://img50.lidatong.com.cn//uploads/allimg/c20201215/52642134740d38d1c3ae568720ce9768.gif)