ActionScript 3鼠绘

本文用AS3分别描绘了曲线、直线、圆、椭圆和矩形5种绘制方法,希望能给大家带来帮助!
(1):曲线
知识点:
1、添加鼠标监听事件MouseEvent;
2、规定画布大小;
3、moveTo和lineTo;
4、绘制开始与结束判断。
代码:
var huabu:Sprite=new Sprite(); 
huabu.graphics.beginFill(0xFFFFFF); 
huabu.graphics.drawRect(20,20,510,360); 
huabu.graphics.endFill(); 
addChild(huabu); 
var _drawing:Boolean; 
var quxian:Sprite=new Sprite(); 
quxian.graphics.lineStyle( 2, 0xFF0000 ); 
huabu.addChild(quxian); 
_drawing = false; 
huabu.addEventListener( MouseEvent.MOUSE_DOWN, startDrawing ); 
huabu.addEventListener( MouseEvent.MOUSE_MOVE,moving); 
huabu.addEventListener( MouseEvent.MOUSE_UP, stopDrawing ); 
stage.addEventListener(MouseEvent.MOUSE_UP,stopDrawing); 
function startDrawing( event:MouseEvent ):void { 
 quxian.graphics.moveTo( mouseX, mouseY ); 
 _drawing = true; 
function moving( event:MouseEvent ):void { 
 if ( _drawing ) { 
  quxian.graphics.lineTo( mouseX, mouseY ); 
 } 
function stopDrawing( event:MouseEvent ):void { 
 _drawing = false; 
}
(2):直线
知识点:
1、添加鼠标监听事件MouseEvent;
2、规定画布大小;
3、moveTo和lineTo;
4、绘制开始与结束判断。
难点:
绘制多条直线
代码:
var huabu:Sprite=new Sprite(); 
huabu.graphics.beginFill(0xFFFFFF); 
huabu.graphics.drawRect(20,20,510,360); 
huabu.graphics.endFill(); 
addChild(huabu); 
var _drawing:Boolean=false; 
var zhixian:Sprite=new Sprite(); 
huabu.addChild(zhixian); 
var _color:Number=0xFF0000; 
var zx:int=1; 
var ys_x:Number; 
var ys_y:Number; 
huabu.addEventListener( MouseEvent.MOUSE_DOWN, startDrawing ); 
huabu.addEventListener( MouseEvent.MOUSE_MOVE,moving); 
huabu.addEventListener( MouseEvent.MOUSE_UP, stopDrawing ); 
stage.addEventListener( MouseEvent.MOUSE_UP, stopDrawing ); 
function startDrawing( event:MouseEvent ):void { 
 ys_x=mouseX; 
 ys_y=mouseY; 
 _drawing = true; 
function moving( event:MouseEvent ):void { 
 if (_drawing) { 
  huabu.removeChild(zhixian); 
  zhixian=new Sprite(); 
  zhixian.graphics.lineStyle( zx, _color ); 
  zhixian.graphics.moveTo(ys_x,ys_y); 
  zhixian.graphics.lineTo(mouseX,mouseY); 
  huabu.addChild(zhixian); 
 } 
function stopDrawing( event:MouseEvent ):void { 
 zhixian=new Sprite(); 
 huabu.addChild(zhixian); 
 //没有以上两句,只能画一条直线 
 _drawing = false; 
}
(3):圆
知识点:
1、添加鼠标监听事件MouseEvent;
2、绘制过程中显示半径(两点间的距离Point.distance(坐标1,坐标2));
3、drawCircle、moveTo和lineTo;
4、绘制开始与结束判断。
难点:
绘制多个圆
代码:
var _drawing:Boolean; 
var yuan:Sprite=new Sprite(); 
addChild(yuan); 
var banjing:Sprite=new Sprite(); 
addChild(banjing); 
var yuanxin_x:Number; 
var yuanxin_y:Number; 
var zuobiao1:Point; 
var zuobiao2:Point; 
var bj:Number; 
_drawing = false; 
stage.addEventListener( MouseEvent.MOUSE_DOWN, startDrawing ); 
stage.addEventListener( MouseEvent.MOUSE_MOVE,yd); 
stage.addEventListener( MouseEvent.MOUSE_UP, stopDrawing ); 
function startDrawing( event:MouseEvent ):void { 
 yuanxin_x=mouseX;//鼠标按下的位置就是圆心的位置 
 yuanxin_y=mouseY; 
 _drawing = true; 
function yd( event:MouseEvent ):void { 
 if ( _drawing ) { 
  zuobiao1 = new Point(yuanxin_x,yuanxin_y); 
  zuobiao2 = new Point(mouseX, mouseY); 
  bj= Point.distance(zuobiao1, zuobiao2);//显示两点之间的距离即为半径 
  removeChild(yuan); 
  yuan=new Sprite(); 
  yuan.graphics.lineStyle( 2, 0xFF0000 ); 
  yuan.graphics.drawCircle(yuanxin_x,yuanxin_y,bj); 
  addChild(yuan); 
  removeChild(banjing); 
  banjing=new Sprite(); 
  banjing.graphics.lineStyle(2,0xFF0000,0.5); 
  banjing.graphics.moveTo(yuanxin_x,yuanxin_y); 
  banjing.graphics.lineTo(mouseX,mouseY); 
  addChild(banjing); 
 } 
function stopDrawing( event:MouseEvent ):void { 
 removeChild(banjing);//绘制结束时半径线消失 
 banjing=new Sprite();//每次画圆过程中都显示半径 
 addChild(banjing); 
 yuan=new Sprite();//可绘制多个圆 
 addChild(yuan); 
  
 _drawing = false; 
}
(4):椭圆
知识点:
1、添加鼠标监听事件MouseEvent和键盘监听事件KeyboardEvent;
2、椭圆的宽与高;
3、drawEllipse;
4、绘制开始与结束判断;
5、按下Shift键绘制正圆。
难点:
按下Shift键绘制正圆
代码:
var _drawing:Boolean=false; 
var ellipse:Sprite=new Sprite(); 
addChild(ellipse); 
var kaishi_x:Number; 
var kaishi_y:Number; 
var w:Number; 
var h:Number; 
var _max:Number; 
stage.addEventListener( MouseEvent.MOUSE_DOWN, startDrawing ); 
stage.addEventListener( MouseEvent.MOUSE_MOVE,moving); 
stage.addEventListener( MouseEvent.MOUSE_UP, stopDrawing ); 
function startDrawing( event:MouseEvent ):void { 
 kaishi_x=mouseX; 
 kaishi_y=mouseY; 
 _drawing = true; 
function moving( event:MouseEvent ):void { 
 if (_drawing) { 
  w=mouseX-kaishi_x; 
  h=mouseY-kaishi_y; 
  removeChild(ellipse); 
  ellipse=new Sprite(); 
  ellipse.graphics.lineStyle( 2, 0xFF0000 ); 
  ellipse.graphics.drawEllipse(kaishi_x,kaishi_y,w,h); 
  addChild(ellipse); 
 } 
function stopDrawing( event:MouseEvent ):void { 
 ellipse=new Sprite(); 
 addChild(ellipse); 
 _drawing = false; 
stage.focus=this; 
stage.addEventListener(KeyboardEvent.KEY_DOWN, shift); 
function shift(event:KeyboardEvent):void { 
 if (event.charCode==0) { 
  if (_drawing) { 
   w=mouseX-kaishi_x; 
   h=mouseY-kaishi_y; 
   _max=Math.max(Math.abs(w),Math.abs(h)); 
   if (w<0 && h<0) { 
    w=-_max; 
    h=-_max; 
   } 
   if (w>0 && h>0) { 
    w=_max; 
    h=_max; 
   } 
   if (w<0 && h>0) { 
    w=-_max; 
    h=_max; 
   } 
   if (w>0 && h<0) { 
    w=_max; 
    h=-_max; 
   } 
   removeChild(ellipse); 
   ellipse=new Sprite(); 
   ellipse.graphics.lineStyle( 2, 0xFF0000 ); 
   ellipse.graphics.drawEllipse(kaishi_x,kaishi_y,w,h); 
   addChild(ellipse); 
  } 
 } 
}
(5):矩形
知识点:
1、添加鼠标监听事件MouseEvent和键盘监听事件KeyboardEvent;
2、椭圆的宽与高;
3、drawRect;
4、绘制开始与结束判断;
5、按下Shift键绘制正方形。
难点:
按下Shift键绘制正方形
代码:
var _drawing:Boolean=false; 
var _drawrect:Sprite=new Sprite(); 
addChild(_drawrect); 
var yx_x:Number; 
var yx_y:Number; 
var w:Number; 
var h:Number; 
var _max:Number; 
stage.addEventListener( MouseEvent.MOUSE_DOWN, startDrawing ); 
stage.addEventListener( MouseEvent.MOUSE_MOVE,moving); 
stage.addEventListener( MouseEvent.MOUSE_UP, stopDrawing ); 
function startDrawing( event:MouseEvent ):void { 
 yx_x=mouseX; 
 yx_y=mouseY; 
 _drawing = true; 
function moving( event:MouseEvent ):void { 
 if (_drawing) { 
  w=mouseX-yx_x; 
  h=mouseY-yx_y; 
  removeChild(_drawrect); 
  _drawrect=new Sprite(); 
  _drawrect.graphics.lineStyle( 2, 0xFF0000 ); 
  _drawrect.graphics.drawRect(yx_x,yx_y,w,h); 
  addChild(_drawrect); 
 } 
function stopDrawing( event:MouseEvent ):void { 
 _drawrect=new Sprite(); 
 addChild(_drawrect); 
 _drawing = false; 
stage.focus=this; 
stage.addEventListener(KeyboardEvent.KEY_DOWN, shift); 
function shift(event:KeyboardEvent):void { 
 if (event.charCode==0) { 
  if (_drawing) { 
   _max=Math.max(Math.abs(w),Math.abs(h)); 
   if (w<0 && h<0) { 
    w=-_max; 
    h=-_max; 
   } 
   if (w>0 && h>0) { 
    w=_max; 
    h=_max; 
   } 
   if (w<0 && h>0) { 
    w=-_max; 
    h=_max; 
   } 
   if (w>0 && h<0) { 
    w=_max; 
    h=-_max; 
   } 
   removeChild(_drawrect); 
   _drawrect=new Sprite(); 
   _drawrect.graphics.lineStyle( 2, 0xFF0000 ); 
   _drawrect.graphics.drawRect(yx_x,yx_y,w,h); 
   addChild(_drawrect); 
  } 
 } 
}
posted @ 2012-02-06 14:23  与时俱进  阅读(375)  评论(0编辑  收藏  举报
友情链接:同里老宅院民居客栈