topical media & game development
actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-DrawingCanvas.ax
actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-DrawingCanvas.ax
[swf]
flex
package //com.example.programmingas3.spritearranger
{
import flash.display.DisplayObject;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import mx.core.UIComponent;
public class @ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-DrawingCanvas extends UIComponent
{
public var bounds:Rectangle;
public var lineColor:Number;
public var fillColor:Number;
public function @ax-actionscript-book-SpriteArranger-com-example-programmingas3-spritearranger-DrawingCanvas(w:Number = 500, h:Number = 200, fillColor:Number = 0xFFFFFF, lineColor:Number = 0x000000)
{
super();
this.bounds = new Rectangle(0, 0, w, h);
this.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
}
public function initCanvas(fillColor:Number = 0xFFFFFF, lineColor:Number = 0x000000):void
{
this.lineColor = lineColor;
this.fillColor = fillColor;
this.width=500;
this.height=200;
drawBounds();
}
public function drawBounds():void
{
this.graphics.clear();
this.graphics.lineStyle(1.0, this.lineColor, 1.0);
this.graphics.beginFill(this.fillColor, 1.0);
this.graphics.drawRect(bounds.left - 1, bounds.top = 1, bounds.width + 2, bounds.height + 2);
this.graphics.endFill();
}
public function addShape(shapeName:String, len:Number):void
{
var newShape:actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite;
switch (shapeName)
{
case "Triangle":
newShape = new actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_TriangleSprite(len);
break;
case "Square":
newShape = new actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_SquareSprite(len);
break;
case "Circle":
newShape = new actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_CircleSprite(len);
break;
}
// makes the shapes slightly transparent, so you can see what's behind them
newShape.alpha = 0.8;
this.addChild(newShape);
}
public function describeChildren():String
{
var desc:String = "";
var child:DisplayObject;
for (var i:int=0; i < this.numChildren; i++)
{
child = this.getChildAt(i);
desc += i + ": " + child + '\n';
}
return desc;
}
public function moveToBack(shape:actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite):void
{
var index:int = this.getChildIndex(shape);
if (index > 0)
{
this.setChildIndex(shape, 0);
}
}
public function moveDown(shape:actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite):void
{
var index:int = this.getChildIndex(shape);
if (index > 0)
{
this.setChildIndex(shape, index - 1);
}
}
public function moveToFront(shape:actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite):void
{
var index:int = this.getChildIndex(shape);
if (index != -1 && index < (this.numChildren - 1))
{
this.setChildIndex(shape, this.numChildren - 1);
}
}
public function moveUp(shape:actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite):void
{
var index:int = this.getChildIndex(shape);
if (index != -1 && index < (this.numChildren - 1))
{
this.setChildIndex(shape, index + 1);
}
}
Traps all mouseUp events and sends them to the selected shape.
Useful when you release the mouse while the selected shape is
underneath another one (which prevents the selected shape from
receiving the mouseUp event).
public function onMouseUp(evt:MouseEvent):void
{
var selectedSprite:actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite = actionscript_book_SpriteArranger_com_example_programmingas3_spritearranger_GeometricSprite.selectedSprite;
if (selectedSprite != null && selectedSprite.isSelected())
{
selectedSprite.onMouseUp(evt);
}
}
}
}
(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.