Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ;
- import flash.display.MovieClip;
- import flash.events.MouseEvent;
- import flash.geom.Point;
- import flash.Lib;
- /**
- * ...
- * @author Corné Dorrestijn
- */
- class Main extends MovieClip
- {
- inline public static var snapSize:Int = 10;
- public var points:Array<Point>;
- public var ssh:Float;
- public var ssw:Float;
- public function new()
- {
- super();
- ssw = Lib.current.stage.stageWidth / 2;
- ssh = Lib.current.stage.stageHeight / 2;
- points = new Array();
- Lib.current.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
- Lib.current.stage.addEventListener(MouseEvent.CLICK, onClick);
- graphics.beginFill(0xFF0000);
- graphics.drawCircle(ssw, ssh, 2);
- }
- public function onClick(e:MouseEvent):Void
- {
- if (points.length > 1)
- {
- var ax = mouseX;
- var ay = mouseY;
- var bx = points[0].x;
- var by = points[0].y;
- if((bx - snapSize < ax && bx + snapSize > ax) && (by - snapSize < ay && by + snapSize > ay))
- {
- ax = bx;
- ay = by;
- Lib.current.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
- Lib.current.stage.removeEventListener(MouseEvent.CLICK, onClick);
- }
- points.push(new Point(ax, ay));
- }else
- points.push(new Point(mouseX, mouseY));
- }
- public function onMouseMove(e:MouseEvent):Void
- {
- if (points.length > 0)
- {
- graphics.clear();
- graphics.beginFill(0xFF0000);
- graphics.drawCircle(ssw, ssh, 2);
- graphics.lineStyle(4, 0xFFFFFF);
- graphics.beginFill(0x454545);
- graphics.beginFill(0xFFFFFF);
- graphics.moveTo(points[0].x, points[0].y);
- if (points.length > 1)
- {
- for (i in 1...points.length)
- graphics.cubicCurveTo(ssw, ssh, ssw, ssh, points[i].x, points[i].y);
- var ax = mouseX;
- var ay = mouseY;
- var bx = points[0].x;
- var by = points[0].y;
- if((bx - snapSize < ax && bx + snapSize > ax) && (by - snapSize < ay && by + snapSize > ay))
- {
- ax = bx;
- ay = by;
- }
- graphics.cubicCurveTo(ssw, ssh, ssw, ssh, ax, ay);
- }else
- graphics.cubicCurveTo(ssw, ssh, ssw, ssh, mouseX, mouseY);
- }
- }
- static function main()
- {
- Lib.current.addChild(new Main());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement