WEB开发网
开发学院图形图像Flash flash 绘图API:绘制基础的图形 阅读

flash 绘图API:绘制基础的图形

 2010-03-02 00:00:00 来源:WEB开发网   
核心提示: 1.stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);2.stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);然后进行处理1.functionmouseD

   1. stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);  
   2. stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);  

然后进行处理

   1. function mouseDownHandler(event:MouseEvent):void  
   2. {  
   3.       
   4.   
   5. }  
   6.   
   7. function mouseMoveHandler(event:MouseEvent):void  
   8. {  
   9.   
  10. }  

基本的设置已经弄好,然后我们对其之前绘制矩形进行封装一个函数方便调用

   1. //绘制这种样式的矩形  
   2. function DrawRect(tx:Number,ty:Number,W:Number,H:Number):void  
   3. {  
   4.     shape.graphics.clear();  
   5.     shape.graphics.beginFill(0x00ff00,0.2);  
   6.     shape.graphics.lineStyle(2);  
   7.     shape.graphics.drawRect(tx,ty, W, H);  
   8.     shape.graphics.endFill();  
   9. } 

四个不同参数,分别是坐标x,y,和矩形大小宽度和高度。

总的代码:当按下鼠标,鼠标拖动的时候,矩形就绘制成了。这个基本的东西已经完成了,不是什么大程序,但是如果你结合到在线图形编辑器,你一定会喜欢上这个。同样我们换成了圆的画法,仅仅修改一个函数就行drawCircle(x,y,r);

要是椭圆,其实仅仅修改一下就行。

   1. var shape:Shape=new Shape();  
   2. addChild(shape);  
   3. var point:Point;  
   4. var key:Boolean=false;  
   5. stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);  
   6. stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);  
   7. function mouseDownHandler(event:MouseEvent):void  
   8. {  
   9.     key=true;  
  10.     point=new Point(mouseX,mouseY);  
  11.     stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMoveHandler);  
  12. }  
  13.   
  14. function mouseMoveHandler(event:MouseEvent):void  
  15. {  
  16.     if (key)  
  17.     {  
  18.         var Width:Number=mouseX-point.x;  
  19.         var Height:Number=mouseY-point.y;  
  20.         DrawRect(point.x,point.y,Width,Height);  
  21.     }  
  22. }  
  23.   
  24. function mouseUpHandler(event:MouseEvent):void  
  25. {  
  26.     key=false;  
  27. }  
  28.   
  29. //绘制这种样式的矩形  
  30. function DrawRect(tx:Number,ty:Number,W:Number,H:Number):void  
  31. {  
  32.     shape.graphics.clear();  
  33.     shape.graphics.beginFill(0x00ff00,0.2);  
  34.     shape.graphics.lineStyle(2);  
  35.     shape.graphics.drawRect(tx,ty, W, H);  
  36.     shape.graphics.endFill();  
  37. }  

Tags:flash 绘图 API

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接