AS3:如何从文档类中调度?
我对从文档类以外的类进行分派有很好的处理能力,但是当我想从文档类分派事件并让其他类侦听文档类广播时会发生什么?
似乎有几种方法可以解决这个问题(即使用单例,使用组合,使用MovieClip(root))我只是想知道人们发现什么是“最佳实践”方法来做到这一点?
I have a pretty good handle on dispatching from classes other than the Document Class, but what happens when I want to dispatch an event from the Document class and have other classes listen to the document class broadcast?
It seems like there are several ways to approach this (i.e using a Singleton, using composition, using MovieClip(root)) I was just wondering what people find is the "best practice" way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
很简单:不要这样做。
为什么你不能直接告诉对象要做什么?文档类位于层次结构的顶部,它应该能够直接操作对象。层次结构中较低的对象应该向上调度,而不是向下调度。
Simple: Don't do it.
Why can't you just tell the objects what to do? The Document Class is at the top of the hierarchy, it should be able to manipulate objects directly. Objects lower down the hierarchy should dispatch up, but not down.
我会这样做:
在文档类中:
如果您不希望其他类访问文档类,我只需将
addEventListener(Event.MY_EVENT, someClass.eventListenerFunction, false, 0, true);
在文档类中。但显然还有其他方法可以做到这一点。取决于您的喜好。
I would do it like this:
in document class:
If you don't want the other classes to have access to the document class, I would just put
addEventListener(Event.MY_EVENT, someClass.eventListenerFunction, false, 0, true);
in the document class.But obviously there's other ways to do it too. Depends on your preference.
您应该将文档类作为参数传递给其他类的构造函数。
You should pass the document class as a paramenter to the others classes constructors.