From 91b0caf974a03adcab02979ceb56904176cb4cca Mon Sep 17 00:00:00 2001 From: Jeremy Penner Date: Mon, 2 May 2011 17:37:45 -0700 Subject: [PATCH] polygonal clickarea --- LaserTube.as3proj | 56 +++++++++++++++++++++++++++++++++ src/ClickArea.as | 80 +++++++++++++++++++++++++++++++++++++++++++++++ src/Main.as | 37 ++++++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 LaserTube.as3proj create mode 100644 src/ClickArea.as create mode 100644 src/Main.as diff --git a/LaserTube.as3proj b/LaserTube.as3proj new file mode 100644 index 0000000..7eb343c --- /dev/null +++ b/LaserTube.as3proj @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ClickArea.as b/src/ClickArea.as new file mode 100644 index 0000000..6632150 --- /dev/null +++ b/src/ClickArea.as @@ -0,0 +1,80 @@ +package +{ + import flash.display.Graphics; + import flash.display.Shape; + import flash.display.Sprite; + import flash.geom.Point; + /** + * Represents a spot on the screen where the user can click. + * @author jjp + */ + public class ClickArea extends Sprite + { + private var shape:Shape; + private var shapeHidden:Shape; + private var fHidden:Boolean; + public function ClickArea(rgpoint: Array, color:uint, alpha:Number = 1) + { + super(); + shape = MakeShape(rgpoint, color, alpha); + shapeHidden = MakeShape(rgpoint, color, 0); + fHidden = false; + addChild(shape); + } + private function MakeShape(rgpoint:Array, color:uint, alpha:Number):Shape + { + var shape:Shape = new Shape(); + shape.graphics.beginFill(color, alpha); + var fFirstPoint:Boolean = true; + for each (var point:Point in rgpoint) + { + if (fFirstPoint) + { + shape.graphics.moveTo(point.x, point.y); + fFirstPoint = false; + } + else + { + shape.graphics.lineTo(point.x, point.y); + } + } + shape.graphics.lineTo(rgpoint[0].x, rgpoint[0].y); + shape.graphics.endFill(); + return shape; + } + public function Show():void + { + if (fHidden) + { + removeChild(shapeHidden); + addChild(shape); + fHidden = false; + } + } + public function Hide():void + { + if (!fHidden) + { + removeChild(shape); + addChild(shapeHidden); + fHidden = true; + } + } + public function Toggle(): void + { + if (fHidden) + Show(); + else + Hide(); + } + public function FHit(point:Point):Boolean + { + var shape:Shape; + if (!fHidden) + shape = this.shape; + else + shape = this.shapeHidden; + return shape.hitTestPoint(point.x, point.y, true); + } + } +} \ No newline at end of file diff --git a/src/Main.as b/src/Main.as new file mode 100644 index 0000000..bbabb85 --- /dev/null +++ b/src/Main.as @@ -0,0 +1,37 @@ +package +{ + import flash.display.Sprite; + import flash.events.Event; + import flash.events.MouseEvent; + import flash.geom.Point; + + /** + * ... + * @author jjp + */ + public class Main extends Sprite + { + private var clickarea:ClickArea; + public function Main():void + { + clickarea = new ClickArea([new Point(50, 50), new Point(100, 80), new Point(30, 120)], 0x555555); + if (stage) init(); + else addEventListener(Event.ADDED_TO_STAGE, init); + } + + private function init(e:Event = null):void + { + removeEventListener(Event.ADDED_TO_STAGE, init); + // entry point + addChild(clickarea); + addEventListener(MouseEvent.CLICK, onClick); + } + private function onClick(e: MouseEvent): void + { + if (clickarea.FHit(new Point(e.stageX, e.stageY))) + clickarea.Toggle(); + } + + } + +} \ No newline at end of file