如何修改现有的AS3事件,以便我可以传递数据? [英] How do I modify existing AS3 events so that I can pass data?

查看:165
本文介绍了如何修改现有的AS3事件,以便我可以传递数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想要一种设置事件的方法,以便我可以传递数据而不会创建闭包\\内存泄漏。这是我所得到的:

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]

我的目标是获得我要传递的自定义数据,以便从事件闪光灯闪光中传递,而不仅仅是我触发的。例如,如果我想要传递一个movieclip以及一个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?

推荐答案

您扩展了Event类以便分配额外的数据,现在如果您希望Loader类分派您的自定义事件类型,请扩展Loader类来执行此操作(或任何其他您想要执行此操作的类)。在这个例子中,我将使用这个功能覆盖URLLoader(因为Loader实际上是从它的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);
        }
    }
}

CustomEvent类在.fla中尝试此代码

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!

这篇关于如何修改现有的AS3事件,以便我可以传递数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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