【发布时间】:2011-04-04 19:33:29
【问题描述】:
我无法同时删除事件侦听器和精灵。我目前收到一个错误:
TypeError:错误 #1009:无法访问 空对象的属性或方法 参考。
如果我注释掉 removeChild,我没有错误,但显然,精灵仍然在屏幕上。有什么想法可以让我自己摆脱这个错误吗?
//Bullet extends Sprite Class
bullet:Bullet = new Bullet();
mc.addChild(bullet);
bullet.addEventListener(Event.ENTER_FRAME, shoot);
function shoot(e:Event):void {
var shot:Bullet = e.currentTarget as Bullet;
//check shot is outside the frame
if (shot.x < 0 - shot.width || shot.x > stage.stageWidth || shot.y > 525)
{
//trying to remove the thing and it's listener
e.currentTarget.removeEventListener(e.type,arguments.callee);
e.currentTarget.parent.removeChild(shot);
}
else
{
shot.setInMotion();
}
}
【问题讨论】:
-
别忘了放入 shot.y
-
只是一点:通常在添加事件监听器时,使用弱引用,即 addEventListener(Event, shoot, false, 0, true),这允许对组件进行垃圾收集,其行为类似于删除事件监听器
标签: actionscript-3 actionscript event-handling removechild