Code to draw a simple rectangle problem

Hi all,

I’m writing an AS3 app to allow the user to draw a simple rectangle shape. The rectangle will start when the user presses the mouse button and complete when they release so the size will be the x and y values between the two points. I’m trying to adapt some code I found from elsewhere but what it’s doing is drawing a new rectangle every time the mouse moves and leaving the previous rectangle visible, hence thousands of rectangles rather than 1. Here is my current code:

package Classes {
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    public class Deckdraw extends MovieClip {
        var deckXStart:int;
        var deckYStart:int;
        public function Deckdraw(){
            init();
        }
        private function init():void{
            trace("deck drawing initialised");
            graphics.lineStyle(2);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
        }
        private function onMouseDownHandler(event:MouseEvent):void{
            
            deckXStart = mouseX;
            deckYStart = mouseY;
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
        }
        private function onMouseUpHandler(event:MouseEvent):void{
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
        }
        private function onMouseMoveHandler(event:MouseEvent):void{
            graphics.moveTo(mouseX, mouseY);
            graphics.drawRect(mouseX, mouseY, deckXStart - mouseX, deckYStart - mouseY);
        }
    }
}

What am I doing wrong???

Many thanks
Kevin