0

我无法同时删除事件侦听器和精灵。我目前收到一个错误:

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();
        }
    }
4

2 回答 2

0

尝试颠倒这些行
也许对 e.currentTarget 的引用通过对象引用丢失了

e.currentTarget.removeEventListener(e.type,arguments.callee);
e.currentTarget.parent.removeChild(shot);

e.currentTarget.parent.removeChild(shot);
e.currentTarget.removeEventListener(e.type,arguments.callee);
于 2011-04-04T21:43:29.377 回答
0

除了在 bullet:Bullet 之前缺少一个var之外,我在示例代码中看不到任何错误。您应该在之后设置断点:

var shot:Bullet = e.currentTarget as Bullet;

并弄清楚为什么shot是空的。我怀疑在您作为示例提供的那一点之外的一段代码中存在一些问题。如果代码只使用注释掉的 removeChild 行,它会告诉我 e.currentTarget 不为 null,但它也不是对 Bullet 类型实例的引用(即“as”强制转换返回 null)。

于 2011-04-04T20:44:30.830 回答