0

I am building a website where I have one page where the user can draw on the screen. Everything works fine except for when I change from the drawing page to another page I get this error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at doodle_fla::MainTimeline/startDrawing()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at doodle_fla::MainTimeline/stopDrawing()

here is my code:

var color:Number;

stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);

function startDrawing(e:MouseEvent):void {
    stage.addEventListener(MouseEvent.MOUSE_MOVE, makeShapes);
    color = Math.random() * 0xFFFFFF;
}

function stopDrawing(e:MouseEvent):void {
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, makeShapes)
}

function makeShapes(e:MouseEvent):void {
        var ellipse:Ellipse = new Ellipse(10, 10, color);
        addChild(ellipse);
        ellipse.x = mouseX;
        ellipse.y = mouseY;
}

How do I clear the stage?

George Profenza
  • 50,687
  • 19
  • 144
  • 218
aldo
  • 1
  • 1
  • 1. When you move to another page, before switching content, remove the startDrawning,stopDrawing event listeners. 2. I would suggest you add the ellipses to a separate Sprite so you can remove all the 'children' clips there, otherwise it will get messy when you'll need to check if instances you're trying to remove are Ellipses or not. You can remove all the child movie clips with something like: while(ellipsesContainer.numChildren) ellipsesContainer.removeChildAt(0); – George Profenza Feb 05 '11 at 00:07

1 Answers1

2
stage.removeEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
stage.removeEventListener(MouseEvent.MOUSE_UP, stopDrawing);

when you leave drawing mode, but you'll have to add them back every time you switch it on

www0z0k
  • 4,444
  • 3
  • 27
  • 32