同时删除事件侦听器和精灵 AS3

发布于 2024-10-30 19:21:50 字数 852 浏览 2 评论 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();
        }
    }

I’m having trouble removing the an event listener as well as the sprite at the same time. I currently get an error:

TypeError: Error #1009: Cannot access
a property or method of a null object
reference.

And if I comment out removeChild, I have no error but, obviously, the sprite remains on the screen. Any ideas how I can rid myself of that error?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

友欢 2024-11-06 19:21:50

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

var shot:Bullet = e.currentTarget as Bullet;

并找出为什么 shot 为空。我怀疑除了您作为示例提供的一小段代码之外,还有一段代码存在问题。如果代码仅使用注释掉的removeChild行,它告诉我e.currentTarget不为null,但它也不是对Bullet类型实例的引用(即“as”强制转换返回null)。

Apart from a missing var before bullet:Bullet, I don't see anything wrong in the example code. You should set a breakpoint right after:

var shot:Bullet = e.currentTarget as Bullet;

And figure out why shot is null. I suspect there is something amiss in a piece of code outside of the little bit you're providing as the example. If the code is working with only the removeChild line commented out, it tells me that e.currentTarget is not null, but that it's also not a reference to an instance of type Bullet (i.e. the "as" cast is returning null).

妄想挽回 2024-11-06 19:21:50

尝试反转这些行
也许对 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);

Try reversing these lines
Maybe the reference to e.currentTarget is getting lost through the object references

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

to

e.currentTarget.parent.removeChild(shot);
e.currentTarget.removeEventListener(e.type,arguments.callee);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文