在 Actionscript3 中创建您自己的事件,它是如何工作的?
我会尽量客观:) 我想知道如何以及为什么有人需要创建您自己的活动。我实际上知道该怎么做,但我不知道做这样的事情的真正想法。您能给我一个例子来解释为什么要创建您自己的活动吗!?
我在网上寻找这个答案,但我从未找到它......
对我的英语感到抱歉:)
I'll try to be objective :)
I would like to know how and why someone will need to create you own event. I actualy know how to do it but I don't get the really idea of do such a thing. Could you give me an example with a explanation of why create your own event!?
I look up for that answer on the web, but I never found it...
And sorry about my english :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我为所有事情创建自己的活动。举例来说,我有一个精灵,它有一个子精灵。子精灵始终根据颜色值数组改变颜色。我想通知某些其他精灵,该精灵现在是红色的,但我不知道它什么时候会发生,因为它正在随机迭代颜色数组。当满足该条件时,子精灵可以触发一个事件,任何正在监听的人都可以对其做出反应。
否则,每个需要知道雪碧何时是红色的人都必须问孩子,你是红色的吗?你是红色的吗?你是红色的吗?
I create my own events for everything. Say for example, I have a Sprite which has a child Sprite. The child Sprite is changing color all the time based on an array of color values. I want to inform certain other sprites, that this sprite is now red but I don't know when it will happen because it is iterating through the color array randomly. The child sprite can fire an event when that condition is met, and anyone who is listening can react upon it.
Otherwise, everyone who needs to know when that Sprite is red will have to ask the child, are you red, are you red, are you red?
我在自定义事件时发表的帖子。
自定义事件只是让您可以更好地控制应用程序中的事件结构。也许最重要的是,语义与这个或那个操作相关:XML 加载成功?触发
MyEvent.XML_LOAD_COMPLETE
。正如我在帖子中详细介绍的那样,您还可以使用新功能扩展自定义事件,特别是在我的例子中,可以处理触发事件旁边的额外数据。
这非常方便。
希望有帮助
A post I made while back on custom events.
Custom events simply give you much more control over the event structure in your app. Maybe most importantly, is the semantic relevance to this or that operation: XML loaded successfully? fire an
MyEvent.XML_LOAD_COMPLETE
.As I detail in the post, you can also extend out custom events with new functionality, specifically, in my case, to handle extra data along side of the fired event.
Which is ridiculously handy.
Hope that helps
当我需要存储有关事件对象的更多信息时,我将创建自己的事件。这样,当我在侦听器中处理事件时,我就可以访问所需的数据,而不必将其存储在 event.target 对象上。
作为旁注,我找到了 信号库 来更好地实现这一点。
I will create my own event when I need to store more information on the event object. That way when I handle the event in the listener I have access to the required data without having to store it on the event.target object.
As a side note I have found the signals library to better achieve this.
让我们尝试将观察者模式与“手动”对象通知系统进行比较......我确信每个人在开始编程时都对此感到内疚。
让我们迭代一下使用此代码会遇到的问题...
1) 我想在另一个游戏 GameY 中使用 Button 类?这是一个问题吗?
当然是,编译时依赖于按钮类中的 GameX...是的,您可以更改它或以其他方式作弊,但这并不容易或标准化...并且标准化是这可能是观察者模式的关键之一......为什么它这么好。这也破坏了封装……稍后会详细介绍。
2) 我想同时监听 GameYComponent1 和 GameYComponent2 的声音!我可以这样做吗?
再次,您可以“作弊”并更改此代码,但是您必须再次更改 Button 类,并且再次,它不会像观察者模式(事件事物)那样标准化。很多时候你根本无法...你能做的最好的事情就是从它继承。工作太多了。也没有标准化。
3)你想要某种非常复杂的事件调度机制,比如as3中的显示对象列表(抱歉,我现在在网上找不到一个好的链接,如果有人稍后可以编辑此内容,请添加链接)。
我可以迭代到明天,有人可以比我更好地解释它,但基本上,不使用观察者模式可能会破坏封装并迫使您一遍又一遍地修改 Button 类......它也不会像事件将被标准化......不需要重新发明轮子,观察者模式的创建就是为了解决这类问题。
无论如何,我都会在 15 分钟内向一个编程新手解释为什么要使用事件或自定义事件。它避免了每个相关人员的很多头痛。
要进行更全面的研究,请尝试这些链接(我只是掩盖了这些内容,但这里应该有一些事实):
如果有人有更多、更好的链接要添加,请随意。
另外,它很好,因为它对封装有一点帮助,Button 类对世界的了解越少,反之亦然越好(无需详细说明,再次,有人可能可以更好地解释这一点):
如果有人有更多、更好的链接要添加,请随意。
另外,我一直提到但不详细,每个有一点经验的人都了解事件并可以跟踪它们。事件可能会让新程序员难以理解代码,并且与具体的参考链接相比更难遵循,但成本大于收益。这就是我说观察者模式的标准化可能是其重要性的关键之一时的意思。
告诉我这对你是否有意义。
Lets try to compare the observer pattern with a "manual" object notification system... I'm sure everybody has been guilty of this when they started programming.
Lets iterate through problems one would encounter with this code...
1) I want to use the Button class in another game, GameY? Is that a problem?
Sure is, compile time dependency on GameX inside the button class... yeah, you could change this or cheat in some other way, but that is not easy or standardized... and standardization is probably one of the key things of the observer pattern... why it's so good. This also breaks encapsulation... more on that in a second.
2) I want to listen from GameYComponent1 and GameYComponent2 at the same time! Can I do it?
Again, you could "cheat" and change this code, but again, you have to change the Button class and, again, it would not be as standardized as the observer pattern (the events thing). And many times you simply cant... the best you can do is subclass from it. To much work. Also not standardized.
3) You want some kind of a very complex event dispatch mechanism, like the display object list in as3 has (sorry, I can't find a good link for this on the net right now, if somebody can edit this later please to add the link in).
I could iterate until tomorrow and there are people that can explain it better than me, but basically, not using the observer pattern will probably break encapsulation and force you to modify the Button class over and over again... it would also not be as standardized as events would be... no need to reinvent the wheel, the observer pattern was created to address these kinds of problems.
In any case, this is how I would explain to a guy new to programming in under 15 minutes why to use events, or custom events. It avoids a lot of headache for everybody involved.
For a more comprehensive study try these links please (I just glossed over these things, but there should be some truth in here):
If somebody has more, better links to add, feel free.
Also, it's good because it helps with encapsulation a little bit, the less Button class knows about the world and vice versa the better (without going into details, and again, somebody can probably explain this better):
If somebody has more, better links to add, feel free.
Also, something I keep mentioning but not in detail, everybody with a little bit of experience understands events and can follow them. Events can make code a pain to understand to new programmers, and are harder to follow versus concrete reference links, but the costs outweigh the benefits. That is what I mean when I say that standardization of the observer pattern is probably one of the keys of it's importance.
Tell me whether it makes sense to you.