我如何清理我的舞台?

时间:2011-02-04 22:59:23

标签: actionscript-3 events listener cs4

我正在建立一个网站,我有一个页面,用户可以在屏幕上绘图。一切正常,除了当我从绘图页面更改为另一个页面时,我收到此错误:

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()

这是我的代码:

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;
}

如何清除舞台?

1 个答案:

答案 0 :(得分:2)

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

当您退出绘图模式时,每次打开它时都必须将它们添加回来