如何修改现有的AS3事件以便我可以传递数据?

时间:2009-04-16 22:39:06

标签: actionscript-3 events listener observer-pattern

所以我想要一种设置事件的方法,这样我就可以在不创建闭包\内存泄漏的情况下传递数据。这是我所拥有的:

package com.events {
    import flash.events.Event;

    public class CustomEvent extends Event {
        public static const REMOVED_FROM_STAGE:String = "removedFromStage";
        public var data:*;

        public function CustomEvent(type:String, customData:*=null, bubbles:Boolean=false, cancelable:Boolean=false) {
            super(type, bubbles, cancelable);
            this.data = customData;
        }

        public override function clone():Event {
            return new CustomEvent(type, data, bubbles, cancelable);
        }

        public override function toString():String {
            return formatToString("CustomEvent", "type", "data", "bubbles", "cancelable", "eventPhase");
        }
    }
}

这让我有以下行为:

function testme(e:Event) {
    trace(e); 
}

test_mc.addEventListener(CustomEvent.REMOVED_FROM_STAGE, testme);
test_mc.dispatchEvent(new CustomEvent(CustomEvent.REMOVED_FROM_STAGE, 42));
//Traces [CustomEvent type="removedFromStage" data=42 bubbles=false cancelable=false eventPhase=2]
removeChild(test_mc);
//Traces [Event type="removedFromStage" bubbles=false cancelable=false eventPhase=2]

我的目标是获取我想要传递的自定义数据以从事件闪光灯传递,而不仅仅是我触发的那个。例如,如果我想传递一个movieclip和一个loader.COMPLETE事件来放置生成的位图,该怎么办?

2 个答案:

答案 0 :(得分:4)

您将Event类扩展为使用额外数据进行调度,现在如果您希望Loader类调度您的自定义事件类型,则扩展Loader类以执行该操作(或者您要执行此操作的任何其他类)。在这个例子中,我将使用这个功能覆盖URLLoader(因为Loader实际上从它的contentLoaderInfo调度事件,它需要两个被覆盖的类,我只想保持简单)


package com.net
{
    import flash.net.URLLoader;
    import flash.events.Event;

    import com.events.CustomEvent;

    public class CustomLoader extends URLLoader
    {
        // URLLoader already has a data property, so I used extraData
        public var extraData:*;

        override public function dispatchEvent(event: Event) : Boolean
        {
            var customEvent: CustomEvent = new CustomEvent(event.type, extraData, event.bubbles, event.cancelable);
            return super.dispatchEvent(customEvent);
        }
    }
}

现在将它与您的CustomEvent类一起使用,在.fla

中尝试此代码

import com.net.CustomLoader;
import com.events.CustomEvent;

var loader: CustomLoader = new CustomLoader();
loader.extraData = "Extra Data";
loader.load(new URLRequest("test.xml"));
loader.addEventListener(Event.COMPLETE, loadComplete);

function loadComplete(event: CustomEvent) : void
{
    trace(event.data); // Extra Data
}

BAM!您天生就派的活动的自定义数据!

答案 1 :(得分:0)

以下显示了创建自定义事件的最简洁方法。通常,事件类型具有在所有国会大写字母中键入的公共静态引用。调度事件时,它会将Event或CustomEvent对象传递给事件处理程序方法。您可以在此处检索传递的值。

package com.hodgedev.events 
{
    import flash.events.Event;

    public class CustomEvent extends Event 
    {
        public static const VALUE_CHANGED:String = "VALUE_CHANGED";
        public var value:Number;

        public function CustomEvent(pValue:Number) 
        { 
            super(CustomEvent.VALUE_CHANGED);
            value = pValue;
        } 
        public override function clone():Event 
        { 
            return new CustomEvent(value);
        }
    }
}

当我们调度事件时,我们创建一个要传递的事件的新实例。

private var _someValue:int = 12;
dispatchEvent(new CustomEvent(_somevalue));