在 AS3 中分派事件时合适的形式是什么?
我想知道创建自定义事件时合适的形式是什么?应该创建一个 CustomEvent 类,然后在函数中创建一个临时调度程序,并调度 CustomEvent。或者最好尝试创建 CustomEventDispatcher 类,并将 CustomEvent 类创建为该类的内部类,例如:
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)
}
}
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)
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
单方面来说,最好让事件公开。通过这种方式,您可以输入侦听器(有利于代码提示和调试),并使事件具有公共静态 const 类型(您也可能想要查看)。
Unilaterally, it is better to make events publicly accessible. This way you can type your listeners (good for code hinting and debugging) and have the Event have public static const types (which you also may want to look in to).
在构思事件机制时,有两个基本问题需要回答。
1) 如何为我的事件创建调度程序实例?
一般选项是:扩展EventDispatcher,或聚合调度程序实例。
最基本和常见的做法(官方文档也指出了这一点)是扩展 EventDispatcher 类,从而为您的类提供事件调度功能。
优点:易于实现——只需输入 extends EventDispatcher 即可完成。
缺点:你不能扩展其他东西。显然,这就是为什么许多本机类是 EventDispatcher 的孙子类的原因。我想只是为了免去我们的麻烦。
第二种通用方法是聚合调度程序实例。
注意:我们将对聚合类的引用传递给调度程序构造函数。
这样做是为了使 event.target 引用您的类实例而不是调度程序实例本身。
优点:您可以自由扩展任何您喜欢的内容。您可以使用调度程序挂钩执行一些技巧,例如维护侦听器列表或类似的操作。
缺点:不像第一种方法那么简单。
2) 如何通过我的事件传递自定义数据?
一般选项是:在事件实例中传递数据,或者仅在事件处理程序中使用 event.target 引用来访问源中的某些数据。
如果您选择通过 event.target 访问所有必需的数据 - 不需要额外的工作,只需将此事件处理程序中的引用转换为适当的类即可。
如果您想随事件一起传递一些数据,则可以对 Event 进行子类化,并且该类应该对处理事件的代码公开可见,如上面的答案所述。 AS3 是关于严格和强类型的,所以你为什么要抗拒呢?
仅当您要重新分派已处理的事件时,才需要重写 Event 子类中的 clone() 方法。官方文档说,为了安全起见,每次创建自定义事件类时都必须这样做。
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.
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.
不要忘记覆盖克隆。重写 toString 进行调试也是一个好主意。
这是我的一个自定义事件的示例:
don't forget to override clone. it's also a good idea to override toString for debugging.
here's an example of one of my custom events: