Flash事件执行顺序

时间:2012-11-28 08:46:03

标签: actionscript-3 flash events

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

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中打开了一个错误。

2 个答案:

答案 0 :(得分:2)

此代码表示Adobe在实际调用事件侦听器之前缓存事件侦听器列表。在一个对象上为一个特定事件提供两个侦听器是一种不寻常的行为,但如果发生这种情况,Adobe会假定在实际修改此列表之前应调用所有侦听器。我实际上期待所有99个听众被召唤。因此,这种行为甚至可以通过设计来实现,因为在处理单个事件时重新呈现事件侦听器列表会给Flash带来太大的负担,因此滞后将会很多。没有人想要滞后。

答案 1 :(得分:0)

通过优先考虑事件监听器,可以轻松解决此问题。 这不是一个Flash bug,它就像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);
    }
}