polygonal clickarea

This commit is contained in:
Jeremy Penner 2011-05-02 17:37:45 -07:00
commit 91b0caf974
3 changed files with 173 additions and 0 deletions

56
LaserTube.as3proj Normal file
View file

@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<!-- Output SWF options -->
<output>
<movie disabled="False" />
<movie input="" />
<movie path="bin/LaserTube.swf" />
<movie fps="30" />
<movie width="800" />
<movie height="600" />
<movie version="10" />
<movie background="#FFFFFF" />
</output>
<!-- Other classes to be compiled into your SWF -->
<classpaths>
<class path="src" />
</classpaths>
<!-- Build options -->
<build>
<option accessible="False" />
<option allowSourcePathOverlap="False" />
<option benchmark="False" />
<option es="False" />
<option loadConfig="" />
<option optimize="True" />
<option showActionScriptWarnings="True" />
<option showBindingWarnings="True" />
<option showDeprecationWarnings="True" />
<option showUnusedTypeSelectorWarnings="True" />
<option strict="True" />
<option useNetwork="True" />
<option useResourceBundleMetadata="True" />
<option warnings="True" />
<option verboseStackTraces="False" />
<option staticLinkRSL="True" />
<option additional="" />
<option customSDK="" />
</build>
<!-- Class files to compile (other referenced classes will automatically be included) -->
<compileTargets>
<compile path="src\Main.as" />
</compileTargets>
<!-- Paths to exclude from the Project Explorer tree -->
<hiddenPaths>
<!-- example: <hidden path="..." /> -->
</hiddenPaths>
<!-- Executed before build -->
<preBuildCommand />
<!-- Executed after build -->
<postBuildCommand alwaysRun="False" />
<!-- Other project options -->
<options>
<option showHiddenPaths="False" />
<option testMovie="Default" />
</options>
</project>

80
src/ClickArea.as Normal file
View file

@ -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);
}
}
}

37
src/Main.as Normal file
View file

@ -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();
}
}
}