Actionscript 3 - 在事件侦听器处理程序中包含一堆代码可以吗?
我正在创建一个 GUI,但只有在一切顺利的情况下,我才能
添加EventListener(Event.Complete, go) 到某些东西并在 go 函数中创建我的 GUI(图形元素,例如标签、列表、方块)?
这样做可以吗?
I'm creating a GUI, but only if things go ok, so can i
addEventListener(Event.Complete, go) to something and in the go function create my GUI (grafical elements such as labels, lists, squares)?
Is it ok to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从技术上来说是没问题的。 crooksy88 给出了一个很好的例子,为事件参数提供默认值以使函数更加通用。
然而,出于语义、清晰度和维护的考虑,我通常更愿意将事物分开更多。所以我的设置可能更像这样:
只需阅读函数名称,就可以更轻松地理解应用程序的流程以及每个部分的功能。当我稍后再回到这个问题时,我会知道我的 UI 是在名为
createUI
的函数中创建的,而不必弄清楚它是在具有像这样的神秘名称的事件处理程序中创建的>go
或handleEvent
。另外,如果我想更改应用程序的流程,例如在创建 UI 之前加载完成后弹出一个对话框,我只需移动一些函数调用,而不是移动大块代码。
Technically it's fine. crooksy88 gives a good example of supplying a default value for the event parameter to make the function more versatile.
However, for the sake of semantics, clarity, and maintenance I would usually prefer to separate things more. So mine might be set up more like this:
It makes it much easier to understand the flow of the app and what each part does just by reading the function names. When I come back to this later, I'll know that my UI is created in the function named
createUI
and not have to figure out that it gets created in an event handler with a cryptic name likego
orhandleEvent
.Also, if I want to change the flow of my app, say to pop up a dialog once the load is complete before the UI is created, I just have to move around some function calls, instead of moving around large chunks of code.
是的,那很好。 go 函数不是事件侦听器的一部分。
上面的示例需要来自侦听器的事件参数 (e:Event)。
但是您可以修改该函数,使参数是可选的,以便您可以随时调用 go 函数。
上面的示例将由侦听器触发,也可以通过键入
go(); 来触发。
Yes that is perfectly fine. The go function isn't part of the event listener.
The sample above requires the event parameter from the listener (e:Event).
But you can modify the function so that the parameter is optional so you can call the go function any time you want
The example above will be triggered by the listener and also by typing
go();