a.addEventListener(MouseEvent.CLICK, doSomething);
b.addEventListener(MouseEvent.CLICK, doSomething);
c.addEventListener(MouseEvent.CLICK, doSomething);
function doSomething(evt:MouseEvent):void
{
var huabu:Sprite=new Sprite();
huabu.graphics.beginFill(0xFFFFFF);
huabu.graphics.drawRect(20,20,510,360);
huabu.graphics.endFill();
addChild(huabu);
var _drawing:Boolean;
var quxian:Sprite=new Sprite();
quxian.graphics.lineStyle( 2, 0xFF0000 );
huabu.addChild(quxian);
if (evt.currentTarget.name == "a")
{
_drawing = false;
huabu.addEventListener( MouseEvent.MOUSE_DOWN, startDrawing );
huabu.addEventListener( MouseEvent.MOUSE_MOVE,moving);
//huabu.addEventListener( MouseEvent.MOUSE_UP, stopDrawing );
stage.addEventListener(MouseEvent.MOUSE_UP,stopDrawing);
function startDrawing( event:MouseEvent ):void
{
quxian.graphics.moveTo( mouseX, mouseY );
_drawing = true;
}
function moving( event:MouseEvent ):void
{
if (_drawing)
{
quxian.graphics.lineTo( mouseX, mouseY );
}
}
function stopDrawing( event:MouseEvent ):void
{
_drawing = false;
}
trace("click button a");
}
else if (evt.currentTarget.name == "b")
{
var _color:Number = 0xFF0000;
var zx:int = 1;
var ys_x:Number;
var ys_y:Number;
huabu.addEventListener( MouseEvent.MOUSE_DOWN, yi );
huabu.addEventListener( MouseEvent.MOUSE_MOVE,er);
huabu.addEventListener( MouseEvent.MOUSE_UP, san );
stage.addEventListener( MouseEvent.MOUSE_UP,san );
function yi( event:MouseEvent ):void
{
ys_x = mouseX;
ys_y = mouseY;
_drawing = true;
}
function er( event:MouseEvent ):void
{
if (_drawing)
{
huabu.removeChild(quxian);
quxian=new Sprite();
quxian.graphics.lineStyle( zx, _color );
quxian.graphics.moveTo(ys_x,ys_y);
quxian.graphics.lineTo(mouseX,mouseY);
huabu.addChild(quxian);
}
}
function san( event:MouseEvent ):void
{
quxian=new Sprite();
huabu.addChild(quxian);
//没有以上两句,只能画一条直线
_drawing = false;
}
trace("click button b");
}
else if (evt.currentTarget.name == "c")
{
var kaishi_x:Number;
var kaishi_y:Number;
var w:Number;
var h:Number;
var _max:Number;
stage.addEventListener( MouseEvent.MOUSE_DOWN, star );
stage.addEventListener( MouseEvent.MOUSE_MOVE,mov);
stage.addEventListener( MouseEvent.MOUSE_UP, stopD );
function star( event:MouseEvent ):void
{
kaishi_x = mouseX;
kaishi_y = mouseY;
_drawing = true;
}
function mov( event:MouseEvent ):void
{
if (_drawing)
{
w = mouseX - kaishi_x;
h = mouseY - kaishi_y;
removeChild(quxian);
quxian=new Sprite();
quxian.graphics.lineStyle( 2, 0xFF0000 );
quxian.graphics.drawEllipse(kaishi_x,kaishi_y,w,h);
addChild(quxian);
}
}
function stopD( event:MouseEvent ):void
{
quxian=new Sprite();
addChild(quxian);
_drawing = false;
}
stage.focus = this;
stage.addEventListener(KeyboardEvent.KEY_DOWN, shift);
function shift(event:KeyboardEvent):void
{
if (event.charCode == 0)
{
if (_drawing)
{
w = mouseX - kaishi_x;
h = mouseY - kaishi_y;
_max = Math.max(Math.abs(w),Math.abs(h));
if (w<0 && h<0)
{
w = - _max;
h = - _max;
}
if (w>0 && h>0)
{
w = _max;
h = _max;
}
if (w<0 && h>0)
{
w = - _max;
h = _max;
}
if (w>0 && h<0)
{
w = _max;
h = - _max;
}
removeChild(quxian);
quxian=new Sprite();
quxian.graphics.lineStyle( 2, 0xFF0000 );
quxian.graphics.drawEllipse(kaishi_x,kaishi_y,w,h);
addChild(quxian);
_drawing = false;
}
}
}
trace("click button c");
}
} |