我在同时删除事件侦听器以及精灵时遇到问题。目前,我得到一个错误:同时删除事件侦听器以及精灵AS3
TypeError: Error #1009: Cannot access a property or method of a null object reference.
如果我注释掉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 < 0 - shot.height :) – 2011-04-04 21:47:02
只是一点:通常当添加事件侦听器时,使用弱引用即addEventListener(Event,shoot,false,0,true),这允许组件被垃圾收集并且类似于删除eventListener – Ryan 2011-04-05 10:53:54