Actionscript 3 - 在事件侦听器处理程序中包含一堆代码可以吗?

发布于 2024-12-26 19:38:48 字数 136 浏览 3 评论 0原文

我正在创建一个 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 技术交流群。

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

发布评论

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

评论(2

雪化雨蝶 2025-01-02 19:38:48

从技术上来说是没问题的。 crooksy88 给出了一个很好的例子,为事件参数提供默认值以使函数更加通用。

然而,出于语义、清晰度和维护的考虑,我通常更愿意将事物分开更多。所以我的设置可能更像这样:

protected function onLoadComplete(e:Event):void {
    initAppSettings();
    createUI();
    startApp();
}

只需阅读函数名称,就可以更轻松地理解应用程序的流程以及每个部分的功能。当我稍后再回到这个问题时,我会知道我的 UI 是在名为 createUI 的函数中创建的,而不必弄清楚它是在具有像 这样的神秘名称的事件处理程序中创建的>gohandleEvent

另外,如果我想更改应用程序的流程,例如在创建 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:

protected function onLoadComplete(e:Event):void {
    initAppSettings();
    createUI();
    startApp();
}

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 like go or handleEvent.

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.

还如梦归 2025-01-02 19:38:48

是的,那很好。 go 函数不是事件侦听器的一部分。

function go(e:Event):void {
// do something
}

上面的示例需要来自侦听器的事件参数 (e:Event)。

但是您可以修改该函数,使参数是可选的,以便您可以随时调用 go 函数。

function go(e:Event = null):void {
// do something
}

上面的示例将由侦听器触发,也可以通过键入

go(); 来触发。

Yes that is perfectly fine. The go function isn't part of the event listener.

function go(e:Event):void {
// do something
}

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

function go(e:Event = null):void {
// do something
}

The example above will be triggered by the listener and also by typing

go();

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