topical media & game development
#graphic-flex-image-effects-01-Flex-DrawingCurves.ax
#graphic-flex-image-effects-01-Flex-DrawingCurves.ax
[swf]
[flash]
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-image-effects-01-Flex-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-image-effects-01-Flex-DrawingCurves() {
_anchor0 = addControlPoint(50, 300);
_anchor1 = addControlPoint(500, 300);
_controlPoint = addControlPoint(275, 100);
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
04/09/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.