请告诉我适当的形式分派在AS3事件什么时候? [英] Whats the appropriate form when dispatching events in AS3?

查看:120
本文介绍了请告诉我适当的形式分派在AS3事件什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么适当的形式创建自定义的事件是什么时候?如果一创建一个自定义事件类,然后创建功能的临时调度,和调度自定义事件。或者是它更好地尝试创建一个CustomEventDispatcher类,并创建自定义事件类作为类的一个内部类,如:

 包
{

   公共类CustomEventDispatcher扩展EventDispatcher
   {
     公共职能CustomEventDispatcher()
     {
       超级(新自定​​义事件());
     }

   }
}

类自定义事件
{
   公共功能自定义事件(类型:字符串,气泡:布尔=假,取消:布尔= FALSE)
   {
      超(类型,泡沫,取消)
   }
}
 

解决方案

有两个基本问题需要回答,构思事件力学的时候。

1)如何创​​建为我的事件调度实例吗?

常规选项有:扩展EventDispatcher,或总调度实例

最基本,最常见的做法(官方的文档还指出),正在扩展EventDispatcher类,从而使你的类的事件调度能力。

优点:实现简单 - 只需键入扩展EventDispatcher,就大功告成了。

缺点:你不能延长别的东西。显然,这就是为什么许多原生类的EventDispatcher的孙子的原因。只是为了饶了我们的麻烦,我猜。

第二一般方法是合并一个调度器的实例。

 包
{
    进口对象类型:flash.events.Event;
    进口flash.events.EventDispatcher使用;
    进口flash.events.IEventDispatcher;

    公共类ClassA的实现IEventDispatcher,请
    {
        私人VAR调度员:EventDispatcher受;

        公共职能ClassA的()
        {
            初始化();
        }

        私有函数初始化():无效
        {
            调度=新的EventDispatcher(本);
        }

        公共职能的addEventListener(类型:字符串,监听器:功能,将useCapture:布尔=假,优先级:= 0,将useWeakReference:布尔=假):无效
        {
            dispatcher.addEventListener(类型,监听器,将useCapture,优先级,将useWeakReference);
        }

        公共职能removeEventListener(类型:字符串,监听器:功能,将useCapture:布尔=假):无效
        {
            dispatcher.removeEventListener(类型,监听器,将useCapture);
        }

        公共职能则dispatchEvent(事件:事件):布尔
        {
            返回dispatcher.dispatchEvent(事件);
        }

        公共职能器,hasEventListener(类型:字符串):布尔
        {
            返回dispatcher.hasEventListener(类型);
        }

        公共职能willTrigger(类型:字符串):布尔
        {
            返回dispatcher.willTrigger(类型);
        }
    }
}
 

请注意:我们通过引用聚合类调度的构造。 这样做是为了将event.target做参考你的类的实例,而不是调度实例本身。

优点:你可以自由地扩展任何你喜欢的。你可以做一些技巧与调度钩子一样保持听众列表或某事的一致好评。

缺点:不是那么简单,第一种方法

2)我如何通过自定义数据与我的事件?

常规选项有:传递数据的事件实例,或者仅使用在事件处理程序参照将event.target从源代码访问的一些数据。

如果您通过选择将event.target访问所有必要的数据 - 没有额外的工作nedded,只投这个参考文献在事件处理程序以适当的类

如果你想通过一些数据与事件,你的子类事件,而这个类应该是公开可见的code处理事件,上述各国的答案。 AS3是所有关于严格强类型,那么,为什么要拒​​绝呢?

如果你要重新调度处理的事件在事件子类中重写clone()方法才是必需的。官方的文档说,你必须做,你创建一个自定义事件类每一次,只要是安全的。

I was wondering what the appropriate form was when creating custom events? Should one create a CustomEvent class, and then create a temporary dispatcher in the function, and dispatch the CustomEvent. or is it better to attempt to create a CustomEventDispatcher class, and create the CustomEvent class as an internal class of that class, eg:

package
{

   public class CustomEventDispatcher extends EventDispatcher
   {
     public function CustomEventDispatcher()
     {
       super(new CustomEvent());
     }

   }
}

class CustomEvent
{
   public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false)
   {
      super(type, bubbles, cancelable)
   }                                               
}

解决方案

There are two basic questions to answer, when conceiving event mechanics.

1) How do I create dispatcher instance for my events?

General options are: extend EventDispatcher, or aggregate dispatcher instance.

Most basic and common practice (and official docs also state that), is extending EventDispatcher class, thus giving your classes event-dispatching capabilities.

Pros: simple to implement -- just type extends EventDispatcher, and you are done.

Cons: you can't extend something else. Apparently, this is the reason why many native classes are EventDispatcher's grandchildren. Just to spare us the trouble, I guess.

Second general approach is aggregating a dispatcher instance.

package
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.IEventDispatcher;

    public class ClassA implements IEventDispatcher
    {
        private var dispatcher:EventDispatcher;

        public function ClassA()
        {
            initialize();
        }

        private function initialize():void
        {
            dispatcher = new EventDispatcher(this);
        }

        public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
        {
            dispatcher.addEventListener(type, listener, useCapture, priority, useWeakReference);
        }

        public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
        {
            dispatcher.removeEventListener(type, listener, useCapture);
        }

        public function dispatchEvent(event:Event):Boolean
        {
            return dispatcher.dispatchEvent(event);
        }

        public function hasEventListener(type:String):Boolean
        {
            return dispatcher.hasEventListener(type);
        }

        public function willTrigger(type:String):Boolean
        {
            return dispatcher.willTrigger(type);
        }
    }
}

Note: we pass a reference to aggregating class to dispatcher constructor. This is done to make event.target reference your class instance and not the dispatcher instance itself.

Pros: you are free to extend whatever you like. You may do some tricks with dispatcher hooks like maintaining listeners list or something alike.

Cons: not as simple as the first approach.

2) How do I pass custom data with my events?

General options are: pass data in an event instance, or only use event.target reference in event handler to access some data from source.

If you choose to access all necessary data through event.target -- no additional work nedded, just cast this reference in event handler to appropriate class.

If you want to pass some data along with event, you subclass Event, and this class should be publicly visible to the code that handles events, as the answer above states. AS3 is all about strict and strong typing, so why would you resist that?

Overriding clone() method in an Event subclass is only necessary if you are going to redispatch handled events. The official docs say you must do that every time you create a custom event class, just to be safe.

这篇关于请告诉我适当的形式分派在AS3事件什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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