Advertisement
cornedor

cubicCurveTo

May 29th, 2011
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package  {
  2.    
  3.     import flash.display.MovieClip;
  4.     import flash.events.MouseEvent;
  5.     import flash.events.Event;
  6.     import flash.geom.Point;
  7.     import flash.display.Graphics; 
  8.    
  9.     public class Main extends MovieClip
  10.     {
  11.        
  12.         private var snapSize:int = 10;
  13.         private var points:Array;
  14.         private var halfStageHeight:Number;
  15.         private var halfStageWidth:Number;
  16.         public function Main() {
  17.            
  18.             halfStageWidth = stage.stageWidth/2;
  19.             halfStageHeight = stage.stageHeight/2
  20.             points = new Array();
  21.             stage.addEventListener(MouseEvent.CLICK, onClick);
  22.             stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  23.             graphics.beginFill(0xFF0000);
  24.             graphics.drawCircle(halfStageWidth, halfStageHeight, 2);
  25.         }
  26.         private function onClick(e:MouseEvent):void
  27.         {
  28.             if(points.length > 1)
  29.             {
  30.                 var ax = mouseX;
  31.                 var ay = mouseY;
  32.                 var bx = points[0].x;
  33.                 var by = points[0].y;
  34.                 if((bx - snapSize < ax && bx + snapSize > ax) && (by - snapSize < ay && by + snapSize > ay))
  35.                 {
  36.                     ax = bx;
  37.                     ay = by;
  38.                     stage.removeEventListener(MouseEvent.CLICK, onClick);
  39.                     stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  40.                 }
  41.                 points.push(new Point(ax, ay));
  42.             }
  43.             else
  44.                 points.push(new Point(mouseX, mouseY));
  45.         }
  46.         private function onMouseMove(e:MouseEvent):void
  47.         {
  48.             if(points.length > 0)
  49.             {
  50.                 graphics.clear();
  51.                 graphics.beginFill(0xFF0000);
  52.                 graphics.drawCircle(halfStageWidth, halfStageHeight, 2);
  53.                 graphics.lineStyle(4, 0xFFFFFF);
  54.                 graphics.beginFill(0xFFFFFF);
  55.                 graphics.moveTo(points[0].x, points[0].y);
  56.                 if(points.length > 1)
  57.                 {
  58.                     for(var i:int = 1; i < points.length; i++)
  59.                         graphics.cubicCurveTo(halfStageWidth, halfStageHeight, halfStageWidth, halfStageHeight, points[i].x, points[i].y);
  60.                        
  61.                     var ax = mouseX;
  62.                     var ay = mouseY;
  63.                     var bx = points[0].x;
  64.                     var by = points[0].y;
  65.                     if((bx - snapSize < ax && bx + snapSize > ax) && (by - snapSize < ay && by + snapSize > ay))
  66.                     {
  67.                         ax = bx;
  68.                         ay = by;
  69.                     }
  70.                     graphics.cubicCurveTo(halfStageWidth, halfStageHeight, halfStageWidth, halfStageHeight, ax, ay);                   
  71.                 }else
  72.                     graphics.cubicCurveTo(halfStageWidth, halfStageHeight, halfStageWidth, halfStageHeight, mouseX, mouseY);
  73.             }
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement