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

发布于 2024-07-17 09:16:59 字数 1342 浏览 3 评论 0原文

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

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 事件来放入生成的位图,该怎么办?

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

This gets me the following behavior:

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]

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

べ映画 2024-07-24 09:16:59

您扩展了 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 类一起使用,请在您的 .砰


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
}

! 有关您固有调度事件的自定义数据!

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

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! Custom data on your innately dispatched events!

难理解 2024-07-24 09:16:59

下面显示了创建自定义事件的最简洁方法。 通常,事件类型具有以所有大写字母键入的公共静态引用。 分派事件时,它会将 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));

The following shows the cleanest way to create a custom event. Typically event types have public static references typed in all capitol letters. When an event is dispatched, it passes an Event, or CustomEvent, object to the event handler method. This is where you can retrieve your passed value.

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

When we dispatch events, we create a new instance of the event to be passed as such.

private var _someValue:int = 12;
dispatchEvent(new CustomEvent(_somevalue));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文