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

2023-09-09 21:44:52 作者:带上耳机╰聆听这世界

所以我想一个办法来设置事件,这样我就可以传递数据,而无需创建倒闭\内存泄漏。这是据我已经得到了:

So I want a way to set up events so that I can pass data without creating closures \ memory leaks. This is as far as I have got:

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]

我的目标是让我想传递给从事件闪光灯闪光,不只是一个,我火获得通过自定义数据。举例来说,如果我想传递一个影片剪辑连同Loader.complete可以活动将生成位图的是什么?

My goal is to get the custom data I want to pass to get passed from the event flash fires, not just the one that I fire. For example, what if I wanted to pass a movieclip along with a loader.COMPLETE event to put the resulting bitmap in?

推荐答案

您扩展事件类为它派遣额外的数据,现在如果你想Loader类调度自定义事件类型,扩展了Loader类做是(或者你想和做其他类)。在这个例子中,我会忽略的URLLoader使用这一功能(因为装载机实际调度事件从它的contentLoaderInfo,这需要两个覆盖类的,我只是想保持简单)

You extended the Event class for it to dispatch with extra data, now if you want the Loader class to dispatch your custom event type, extend the Loader class to do that (or any other class you want to do this with). In this example I'll override URLLoader with this functionality (because Loader actually dispatches events from it's contentLoaderInfo, which needs two overridden classes, and I just want to keep it simple)


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

现在使用这个与你的自定义事件类试试这个code。在你的根

Now to use this with your CustomEvent class try this code in your .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!在你的天生调度的事件自定义的数据!

BAM! Custom data on your innately dispatched events!