4

查看下一个代码,告诉我您希望打印什么。然后运行它并检查实际发生的情况。

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class TestFlashEvents extends Sprite
    {
        private static const DUMMY_EVENT:String = "DummyEvent";
        private var dummyObjects:Vector.<DummyObject> = new Vector.<DummyObject>(100);
        public function TestFlashEvents()
        {
            for(var i:int = 0; i < dummyObjects.length; i++){
                dummyObjects[i] = new DummyObject(this);
                addEventListener(DUMMY_EVENT, dummyObjects[i].listener);
            }
            removeEventListener(DUMMY_EVENT, dummyObjects[41].listener);
            dispatchEvent(new Event(DUMMY_EVENT));
        }

        private var counter:int = 0;
        public function onGettingEvent(dummyObject:DummyObject):void{
            if(counter == 25){
                for(var i:int = 0; i < 50; i++){
                    removeEventListener(DUMMY_EVENT, dummyObjects[i].listener);
                    trace("Removing", dummyObjects[i].id);
                }
            }
            trace("Handeling event", counter, dummyObject.id);
            counter++;
        }
    }
}
import flash.events.Event;

class DummyObject
{
    private static var dummyObjectsCounter:int = 0;

    public var id:String;
    private var tester:TestFlashEvents;

    public function DummyObject(tester:TestFlashEvents)
    {
        this.tester = tester;
        id = "DummyObject " + dummyObjectsCounter;
        dummyObjectsCounter++;
    }

    public function listener(event:Event):void{
        tester.onGettingEvent(this);
    }
}

removeEventListener功能实际上不起作用。告诉我你怎么看。我还在adobe中打开了一个错误。

4

2 回答 2

2

此代码意味着 Adob​​e 在实际调用事件侦听器之前缓存事件侦听器列表。在一个对象上为一个特定事件设置两个侦听器是一种不寻常的行为,但如果发生这种情况,Adobe 假定应该在实际修改此列表之前调用所有侦听器。我实际上期待所有 99 位听众都被叫到。因此,这种行为甚至可以是设计使然,因为在处理单个事件时重新渲染事件侦听器列表会给 Flash 带来过重的负载,从而导致延迟过多。没有人想要滞后。

于 2012-11-28T09:35:34.980 回答
0

这可以通过优先处理事件侦听器来轻松解决。这不是 Flash 错误,它就像 Vesper 所说的缓存事件侦听器以防止延迟。

这是您的代码的更新版本,请在addEventListener调用中标记优先级。

package
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class TestFlashEvents extends Sprite
    {
        private var _this = this;
        private static const DUMMY_EVENT:String = "DummyEvent";
        private var dummyObjects:Vector.<DummyObject> = new Vector.<DummyObject>(100);
        public function TestFlashEvents()
        {
            for(var i:int = 0; i < dummyObjects.length; i++)
            {
                dummyObjects[i] = new DummyObject(this);
                addEventListener(DUMMY_EVENT, dummyObjects[i].listener, false, dummyObjects.length - i);
            }
            removeEventListener(DUMMY_EVENT, dummyObjects[41].listener);
            dispatchEvent(new Event(DUMMY_EVENT));
        }

        private var counter:int = 0;
        public function onGettingEvent(dummyObject:DummyObject):void
        {
            if (counter == 25)
            {
                for (var i:int = 0; i < 50; i++)
                {
                    removeEventListener(DUMMY_EVENT, dummyObjects[i].listener);
                    trace("Removing", dummyObjects[i].id);
                }
            }
            trace("Handeling event", counter, dummyObject.id);
            counter++;
        }
    }
}

import flash.events.Event;

class DummyObject
{
    private static var dummyObjectsCounter:int = 0;

    public var id:String;
    private var tester:TestFlashEvents;

    public function DummyObject(tester:TestFlashEvents)
    {
        this.tester = tester;
        id = "DummyObject " + dummyObjectsCounter;
        dummyObjectsCounter++;
    }

    public function listener(event:Event):void{
        tester.onGettingEvent(this);
    }
}
于 2012-12-06T10:18:59.630 回答