topical media & game development

talk show tell print

graphic-flex-draw-classes-DrawingCurves.ax

graphic-flex-draw-classes-DrawingCurves.ax [swf] flex


  package  {
  
          import flash.display.Sprite;
          import flash.events.MouseEvent;
  
          [SWF(width=550, height=400, backgroundColor=0xFFFFFF)]
  
          
Demonstrates the quadtratic curve of the drawing API, allowing user to drag control point and anchors.

  
          public class @ax-graphic-flex-draw-classes-DrawingCurves extends Sprite {
  
                  private var _controlPoint:Sprite;
                  private var _anchor0:Sprite;
                  private var _anchor1:Sprite;
  
                  
Constructor. Creates anchors and control point and draws initial curve.

  
                  public function @ax-graphic-flex-draw-classes-DrawingCurves() {
                          _anchor0 = addControlPoint(50, 300);
                          _anchor1 = addControlPoint(500, 300);
                          _controlPoint = addControlPoint(275, 300);
                          drawCurve();
                  }
  
                  
Adds a draggable control point to the stage.
parameter: x The x position of the control point.
parameter: y The y position of the control point.
returns: The Sprite instance representing the control point.

  
                  private function addControlPoint(x:Number, y:Number):Sprite {
                          var controlPoint:Sprite = new Sprite();
                          // draw a big stroke thickness with little length to create a circle
                          controlPoint.graphics.lineStyle(20);
                          controlPoint.graphics.lineTo(1, 0);
                          controlPoint.addEventListener(MouseEvent.MOUSE_DOWN, onControlDown);
                          controlPoint.addEventListener(MouseEvent.MOUSE_UP, onControlUp);
                          controlPoint.x = x;
                          controlPoint.y = y;
                          addChild(controlPoint);
                          return controlPoint;
                  }
  
                  
Redraws the curve based on the current position of the points.

  
                  private function drawCurve():void {
                          graphics.clear();
                          graphics.lineStyle(3, 0xFF);
                          graphics.moveTo(_anchor0.x, _anchor0.y);
                          graphics.curveTo(_controlPoint.x, _controlPoint.y, _anchor1.x, _anchor1.y);
                          graphics.lineStyle(1, 0, .5);
                          graphics.lineTo(_controlPoint.x, _controlPoint.y);
                          graphics.lineTo(_anchor0.x, _anchor0.y);
                  }
  
                  
Handler for when a control point or anchor is clicked. This begins the drag of the point and forces a draw update on move.
parameter: event Event dispatched by control point sprite.

  
                  private function onControlDown(event:MouseEvent):void {
                          (event.target as Sprite).startDrag();
                          stage.addEventListener(MouseEvent.MOUSE_MOVE, onControlMove);
                  }
  
                  
Handler for when a control point or anchor is released. This stops the drag and removes the MOUSE_MOVE listener.
parameter: event Event dispatched by control point sprite.

  
                  private function onControlUp(event:MouseEvent):void {
                          (event.target as Sprite).stopDrag();
                          stage.removeEventListener(MouseEvent.MOUSE_MOVE, onControlMove);
                  }
  
                  
Handler for when a control point or anchor is dragged. This forces a redraw of the curve.
parameter: event Event dispatched by stage.

  
                  private function onControlMove(event:MouseEvent):void {
                          drawCurve();
                          event.updateAfterEvent();
                  }
  
          }
  
  }
  


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.