topical media & game development
graphic-flex-draw-classes-CopyingGraphics.ax
graphic-flex-draw-classes-CopyingGraphics.ax
[swf]
flex
package {
import flash.events.*;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.*;
import flash.filters.GlowFilter;
import flash.ui.Keyboard;
import mx.containers.Canvas;
[SWF(width=550, height=550, backgroundColor=0)]
Copies a single shape's vector drawings into several other shapes rotated
around a common point.
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.*;
import flash.filters.GlowFilter;
import flash.ui.Keyboard;
public class @ax-graphic-flex-draw-classes-CopyingGraphics extends Sprite {
private static const INIT_SEGMENTS:uint = 10;
private static const MAX_SEGMENTS:uint = 20;
private static const MIN_SEGMENTS:uint = 3;
private static const THICKNESS:uint = 1;
private static const COLOR:uint = 0x66CCCC;
private static const ROTATION_RATE:uint = 1;
private var _shapeHolder:Sprite;
private var _shapes:Vector.<Shape>;
private var _x:Number;
private var _y:Number;
private var _canvas:Canvas = new Canvas;
Constructor.
public function @ax-graphic-flex-draw-classes-CopyingGraphics(x:Number, y:Number, canvas:Canvas) {
super();
_x = x;
_y = y;
_canvas = canvas
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
this.init();
}
Initializes shapes and sets up stage listeners.
public function init():void {
_shapeHolder = new Sprite();
//_shapeHolder.x = stage.stageWidth/2;
//_shapeHolder.y = stage.stageHeight/2;
_shapeHolder.x = _x;;
_shapeHolder.y = _y;
addChild(_shapeHolder);
_shapes = new Vector.<Shape>();
for (var i:uint = 0; i < INIT_SEGMENTS; i++) {
addSegment();
}
positionSegments();
_canvas.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
_canvas.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onStageKeyDown);
filters = [new GlowFilter(COLOR)];
}
Draws a new line in the master shape and copies the master shape's drawing
into the other shapes in the vector array.
private function draw():void {
var shape:Shape = _shapes[0];
shape.graphics.lineTo(shape.mouseX, shape.mouseY);
var segments:uint = _shapeHolder.numChildren;
// when updating, skip initial shape since that is what is being copied
for (var i:uint = 1; i < segments; i++) {
_shapes[i].graphics.copyFrom(shape.graphics);
}
}
Adds a new shape to the number of rotated shapes.
private function addSegment():void {
var shape:Shape = new Shape();
// for any but initial shape, simply copy the graphics from the initial shape
if (_shapes.length > 0) {
shape.graphics.copyFrom(_shapes[0].graphics);
// initial shape just needs a linestyle
} else {
shape.graphics.lineStyle(THICKNESS, COLOR);
}
_shapes.push(shape);
_shapeHolder.addChild(shape);
}
Removes a segment from the end of the rotated shapes vector.
private function removeSegment():void {
var shape:Shape = _shapes.pop();
_shapeHolder.removeChild(shape);
}
Rotates all the segments so that they are placed at equal angles around central point.
private function positionSegments():void {
var segments:uint = _shapeHolder.numChildren;
var angle:Number = 360/segments;
for (var i:uint = 1; i < segments; i++) {
_shapes[i].rotation = angle*i;
}
}
Handler for when the stage is clicked. This moves the pen in the master shape to
the clicked position and sets up a listener for ENTER_FRAME.
parameter: event The event dispatched by the stage.
private function onStageMouseDown(event:MouseEvent):void {
var shape:Shape = _shapes[0];
if(shape.mouseX > _canvas.width || shape.mouseY > _canvas.height){
shape.graphics.moveTo(Math.min(shape.mouseX, _canvas.width), Math.min(shape.mouseY,_canvas.height));
} else shape.graphics.moveTo(shape.mouseX, shape.mouseY);
addEventListener(Event.ENTER_FRAME, onThisEnterFrame);
}
Handler for when the mouse is released on the stage.
This removes the listener for ENTER_FRAME.
parameter: event The event dispatched by the stage.
private function onStageMouseUp(event:MouseEvent):void {
removeEventListener(Event.ENTER_FRAME, onThisEnterFrame);
}
Handler for the ENTER_FRAME event. This rotates all of the shapes and updates the drawing.
parameter: event The event dispatched by this sprite.
private function onThisEnterFrame(event:Event):void {
if(mouseX > _canvas.width || mouseY > _canvas.height){
} else {
_shapeHolder.rotation += ROTATION_RATE;
draw();
}
}
Handler for when a key is pressed. If it is the UP or DOWN arrow, the number of segments
is increased or decreased.
parameter: event The event dispatched by the stage.
private function onStageKeyDown(event:KeyboardEvent):void {
switch (event.keyCode) {
// for UP arrow key, add a segment if we are within limit
case Keyboard.UP:
if (_shapeHolder.numChildren < MAX_SEGMENTS) {
addSegment();
positionSegments();
}
break;
// for DOWN arrow key, remove a segment if we are within limit
case Keyboard.DOWN:
if (_shapeHolder.numChildren > MIN_SEGMENTS) {
removeSegment();
positionSegments();
}
break;
}
}
}
}
(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.