topical media & game development
actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite.ax
actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite.ax
[swf]
flex
package // com.example.programmingas3.spritearranger
{
//import com.example.programmingas3.geometricshapes.IGeometricShape;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
public class @ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite extends Sprite
{
public var size:Number;
public var lineColor:Number = 0x000000;
public var fillColor:Number = 0xDDDDEE;
public var shapeType:String = "GeometricSprite";
An instance of a purely geometric shape, that is, one that defines
a shape mathematically but not visually.
public var geometricShape:actionscript_book_SpriteArranger_com_example_programmingas3_geometricshapes_IGeometricShape;
Keeps track of the currently selected shape.
This is a static property, so there can only be one @ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite
selected at any given time.
public static var selectedSprite:@ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite;
Holds a border rectangle that is shown when this @ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite instance is selected.
public var selectionIndicator:Shape;
public function @ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite(size:Number = 100, lColor:Number = 0x000000, fColor:Number = 0xDDDDEE)
{
this.size = size;
this.lineColor = lColor;
this.fillColor = fColor;
this.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
public function drawShape():void
{
// to be overridden in subclasses
}
private function onMouseDown(evt:MouseEvent):void
{
this.showSelected();
// limits dragging to the area inside the canvas
var boundsRect:Rectangle = this.parent.getRect(this.parent);
boundsRect.width -= this.size;
boundsRect.height -= this.size;
this.startDrag(false, boundsRect);
}
public function onMouseUp(evt:MouseEvent):void
{
this.stopDrag();
}
private function showSelected():void
{
if (this.selectionIndicator == null)
{
// draws a red rectangle around the selected shape
this.selectionIndicator = new Shape();
this.selectionIndicator.graphics.lineStyle(1.0, 0xFF0000, 1.0);
this.selectionIndicator.graphics.drawRect(-1, -1, this.size + 1, this.size + 1);
this.addChild(this.selectionIndicator);
}
else
{
this.selectionIndicator.visible = true;
}
if (@ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite.selectedSprite != this)
{
if (@ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite.selectedSprite != null)
{
@ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite.selectedSprite.hideSelected();
}
@ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-GeometricSprite.selectedSprite = this;
}
}
private function hideSelected():void
{
if (this.selectionIndicator != null)
{
this.selectionIndicator.visible = false;
}
}
Returns true if this shape's selection rectangle is currently showing.
public function isSelected():Boolean
{
return !(this.selectionIndicator == null || this.selectionIndicator.visible == false);
}
public override function toString():String
{
return this.shapeType + " of size " + this.size + " at " + this.x + ", " + this.y;
}
}
}
(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.