13Feb

[AS 3] Recreating the Drawing Tool with Actionscript 3

No comments

Simply press and drag your mouse around the flash movie below.

package
{
	import flash.display.Sprite;
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	import flash.events.Event;

	public class DrawingTool extends Sprite
	{
		private var drawing:Boolean;

		public function DrawingTool()
		{
			graphics.lineStyle(1,0xffffff);
			graphics.moveTo(mouseX,mouseY);

			stage.addEventListener(MouseEvent.MOUSE_DOWN, MouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, MouseUp);

			addEventListener(Event.ENTER_FRAME, Drawing);
		}

		private function MouseDown(m:MouseEvent):void
		{
			graphics.lineStyle(1,Math.random() * 0xffffff);
			drawing = true;
		}

		private function MouseUp(m:MouseEvent):void
		{
			drawing = false;
		}

		private function Drawing(e:Event):void
		{
			if (drawing)
			{
				this.graphics.lineTo(mouseX, mouseY);
			}
			else
			{
				this.graphics.moveTo(mouseX, mouseY);
			}
		}
	}
}

Share/Save/Bookmark

Wednesday, February 13th, 2008 at 10:03 am and is filed under Actionscript 3, Drawing API. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply