如何在AS3中一次删除所有事件侦听器 [英] How to delete all event listeners at once in AS3

查看:1400
本文介绍了如何在AS3中一次删除所有事件侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用as3做一个小游戏.

I'm making a small game in as3.

游戏包含10个级别.

当我进入1级时,一切都很好.但是,当我进入第二级(帧)时,第一帧中的事件侦听器仍在工作,并且收到一条警告,提示无法访问空对象引用的对象".这是因为我删除了第一级的每个对象,然后从阶段2中添加了对象.

When i enter 1 level everything is alright. But when i enter the second level (frame) the event listeners from the first frame are still working and a recieve a warning saying ' Cannot access an object of null objct reference'. This is because i delete every object of the first level and th add the objects from stage 2.

我尝试使用removeEventListeners,但是它不起作用,因为在删除事件侦听器后,ENTER_FRAME侦听器又工作了一次.

I've tried using removeEventListeners, but it doesn't work, cause ENTER_FRAME Listeners work one more time after I remove the Event Listeners.

我尝试过将不同的帧用于不同的级别,有点不起作用.我也尝试对所有10帧使用1 frmae,但是我收到很多警告,并且Flash Loader过载.

I've tried using different frames for different levels, bit it doesn't work. Also i tried using 1 frmae for all 10 frames, but i recieve much many warning and the Flash Loader is overloaded.

如何切换级别(前进和后退)?预先感谢.

How can i switch through levels (back and forward)? Thanks in advance.

  addEventListener(Event.ENTER_FRAME, subtracting2);
     arrListeners.pop(); // poping it out of the array because it will be deleted after the count reaches 0
     function subtracting2 (e:Event):void
     {
        count--;
        var FAcoef:Number = count/30; //
        FadeAway.alpha = FAcoef; //                   Some effect like FadeAway
        setChildIndex(FadeAway, numChildren - 1); //
        if(count == 0)
       {
            setChildIndex(FadeAway, 0);
            removeEventListener(Event.ENTER_FRAME, subtracting2);
        }
    }

推荐答案

没有内置的方法来删除所有侦听器.

There is no built-in way to remove all listeners.

当对象被垃圾收集时,可以使用弱引用来删除侦听器.

You could use weak references to let the listeners be removed when the object is Garbage Collected.

object.addEventListener(......,.......,false,0,true);

object.addEventListener( ......, ......., false, 0, true );

或者您可以自己添加removeAllListeners功能,这是一些信息:
http://blog.reyco1.com/method-of-removing-所有事件侦听器/(看看离子注释)

Or you could add the removeAllListeners functionality yourself, here is some info:
http://blog.reyco1.com/method-of-removing-all-event-listeners/ (Have a look at Ion comment)

但是,如果您要立即删除不再需要的每个事件侦听器,则不需要上述任何内容.

But.. you shouldn't need any of the above if you take care to remove every event listener straight away when it is not needed any more.

如果您的类具有一个或多个事件侦听器,而这些侦听器在实例生命周期结束之前仍需要使用,则应创建一个destroy()函数.在该destroy()函数中,您将删除所有事件侦听器.

If you have a class with one or more event listeners which are needed till the end of the instance's life, you should create a destroy() function. In that destroy() function you would remove all the event listeners.

在您的情况下,可以在转到第二级(帧)之前调用destroy().

In your case, you could call destroy() before you go to second level(frame).

这篇关于如何在AS3中一次删除所有事件侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆