2011-02-04 59 views
0

我正在建立一个网站,我有一个页面,用户可以在屏幕上绘制。一切正常,除非我从绘图页切换到另一个页面,我得到这个错误:如何清理我的舞台?

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

如何清空舞台?

+0

1.当你移动到另一个页面,切换内容之前的时间加回,取出startDrawning,stopDrawing事件听众。 2.我建议你将椭圆添加到一个单独的Sprite中,这样你就可以删除所有的'children'剪辑,否则当你需要检查你试图删除的实例是否是Ellipses时会变得混乱。您可以删除所有类似的子影片:while(ellipsesContainer.numChildren)ellipsesContainer.removeChildAt(0); – 2011-02-05 00:07:30

回答

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

当你离开绘图模式,但你必须给他们每次开机