# Code to draw a simple rectangle problem

**URL:** <https://forum.kirupa.com/t/code-to-draw-a-simple-rectangle-problem/268759>\
**Category:** flash\
**Created:** [August 18, 2008, 3:33pm UTC](https://forum.kirupa.com/t/code-to-draw-a-simple-rectangle-problem/268759 "2008-08-18T15:33:51Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![the\_lar](https://avatars.discourse-cdn.com/v4/letter/t/90ced4/32.png) [@the\_lar](https://forum.kirupa.com/u/the_lar)\
**Post date:** [August 18, 2008, 3:33pm UTC](https://forum.kirupa.com/t/code-to-draw-a-simple-rectangle-problem/268759/1 "2008-08-18T15:33:51Z")

</div>

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:

```auto
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
