何时扩展 EventDispatcher

发布于 2024-09-13 06:10:37 字数 150 浏览 8 评论 0原文

简单的问题..我想知道你们什么时候在班级中扩展 EventDispatcher 。在我看来,只要我们有导入事件包,我们就可以毫无问题地调度事件......我有时看到人们在他们的类中扩展EventDispatcher......不知道为什么......有人愿意解释一下吗?谢谢一百万...

Simple question..I was wondering when you guys extends EventDispatcher in your class. It seems to me that as long as we have import event package, we can dispatchEvent without problems....I saw sometime people extends EventDispatcher in their class...not sure why...anyone care to explain? Thanks a million...

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

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

发布评论

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

评论(1

冬天旳寂寞 2024-09-20 06:10:52

我认为您可能会感到困惑,AS3 中的许多对象在继承树中扩展了 EventDispatcher,只需要导入 flash.events 包即可分派事件。例如,许多 DisplayObject 类扩展了 EventDispatcher。这里有几个例子:

Shape » DisplayObject » EventDispatcher » Object

Sprite » DisplayObjectContainer » InteractiveObject » DisplayObject » EventDispatcher » Object

通常,每当我使用自定义类时,我都会扩展 EventDispatcher,该自定义类只需要与其范围之外的对象进行通信,了解某些内部属性已更改或某些功能正在发生。这是一个例子:

public class Clock extends EventDispatcher
{
     protected var _tick:uint;

     protected function run():void
     {
         if( _tick + 1 > 60 ) {
             _tick = 0;
         } else {
             _tick++;
         }
         dispatchEvent( new Event( Event.CHANGE ) );
     }

     public function getTick():uint { return _tick; }
}

有时保持对象的内部细节只读是“重要的”。在上面的示例中,当调用 run() 方法时,Clock 类执行一些内部逻辑,然后调度一个事件来指示某些内容已发生更改。然后,任何侦听该事件的类都可以调用公共 getTick() 方法来查找 _tick 的值。这隐藏了实现并保护 _tick 变量不被外部类更改,同时提供了一个可以读取 Clock 的接口。

I think you might be confusing the fact that many objects in AS3 extend EventDispatcher higher in the inheritance tree with only needing to import the flash.events package in order to dispatch events. For instance many DisplayObject classes extend the EventDispatcher. Here are a couple of examples:

Shape » DisplayObject » EventDispatcher » Object

Sprite » DisplayObjectContainer » InteractiveObject » DisplayObject » EventDispatcher » Object

Typically I will extend EventDispatcher any time I am working with a custom class that just needs to communicate to objects outside of it's scope that some internal property has changed or that some function is occuring. Here is an example:

public class Clock extends EventDispatcher
{
     protected var _tick:uint;

     protected function run():void
     {
         if( _tick + 1 > 60 ) {
             _tick = 0;
         } else {
             _tick++;
         }
         dispatchEvent( new Event( Event.CHANGE ) );
     }

     public function getTick():uint { return _tick; }
}

Sometimes it is "important" to keep the internal details of an object read only. In the case of the above example, when the run() method is called, the Clock class performs some internal logic and then dispatches an event indicating that something has changed. Any class that is listening for that event can then call the public getTick() method to find out the value of _tick. This hides the implementation and protects the _tick variable from being changed by outside classes and at the same time provides an interface through which the Clock can be read.

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