Friday, December 18, 2009

Draw rectangle with mouse

// draw rectangle using mouse move

var isDrag:Boolean = false;
var _point:Point;
const rect:Graphics = graphics;
//
stage.addEventListener(MouseEvent.MOUSE_DOWN, startRect);
stage.addEventListener(MouseEvent.MOUSE_UP, completeRect);
//
function startRect(e:MouseEvent):void{
if(!isDrag){
rect.clear();
_point = new Point(e.stageX, e.stageY);
isDrag = true;
stage.addEventListener(MouseEvent.MOUSE_MOVE, showDrag);
}
}
//
function completeRect(e:MouseEvent):void{
if(isDrag){
isDrag = false;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, showDrag);
rect.lineStyle(2,0x000000);
rect.beginFill(0xFF0000, 1);
rect.drawRect(_point.x, _point.y, e.stageX - _point.x, e.stageY - _point.y);
}
}
//
function showDrag(e:MouseEvent):void{
rect.clear();
rect.lineStyle(2, 0x000000);
rect.drawRect(_point.x, _point.y, e.stageX - _point.x, e.stageY - _point.y);
}

No comments:

Post a Comment