topical media & game development
lib-flex-animation-code-08-PathSketch.ax
lib-flex-animation-code-08-PathSketch.ax
(swf
)
[ flash
]
flex
package
{
import __AS3__.vec.Vector;
import flash.display.GraphicsPathCommand;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
[SWF(backgroundColor=0xffffff)]
public class @ax-lib-flex-animation-code-08-PathSketch extends Sprite
{
private var commands:Vector.<int> = new Vector.<int>();
private var data:Vector.<Number> = new Vector.<Number>();
private var lineWidth:Number = 0;
private var lineColor:uint = 0;
public function @ax-lib-flex-animation-code-08-PathSketch()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onMouseDown(event:MouseEvent):void
{
commands.push(GraphicsPathCommand.MOVE_TO);
data.push(mouseX, mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
draw();
}
private function onMouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
private function onMouseMove(event:MouseEvent):void
{
commands.push(GraphicsPathCommand.LINE_TO);
data.push(mouseX, mouseY);
draw();
}
private function onKeyUp(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.DOWN)
{
lineWidth = Math.max(0, lineWidth -1);
}
else if(event.keyCode == Keyboard.UP)
{
lineWidth++;
}
else if(event.keyCode == Keyboard.SPACE)
{
lineColor = Math.random() * 0xffffff;
}
draw();
}
private function draw():void
{
graphics.clear();
graphics.lineStyle(lineWidth, lineColor);
graphics.drawPath(commands, data);
}
}
}
(C) Æliens
18/6/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.