使用 Haxe 定位 Flash 的工具栏事件处理

发布于 2024-07-29 06:57:37 字数 1141 浏览 3 评论 0原文

我正在 Haxe 中创建一个简单的 UI 工具栏组件,以便在 Flash 游戏中使用。 工具栏是一般UI风格的工具栏,它有一个背景和几个按钮。 单击按钮时会发生一些游戏动作。

我有一个关于实现工具栏按钮事件的最佳方法的问题。 父类将创建一个工具栏,然后将它想要的任何功能注册到单击事件上的按钮。

我当前的解决方案如下所示:

  • 工具栏扩展了精灵类。
  • 工具栏类的每个按钮都有一个子精灵。
  • 每个按钮都有一个 MouseEvent.Click 事件的事件侦听器。
  • MouseEvent.Click 由工具栏上的函数处理,该函数检查动态类型数组并执行数组中包含的每个函数。
  • 动态类型数组是公共的,因此任何想要注册到工具栏的类 按钮上的点击事件可以将其回调函数添加到数组中。

具有工具栏的场景的代码片段

this.toolbar = new ToolBar();
this.toolbar.OnAttackButtonClicked.push(OnAttackButtonClicked);

用于在工具栏中创建按钮的代码

this.attackButton = new Sprite();
this.attackButton.addEventListener(MouseEvent.CLICK, this.OnAttackButtonClickListener);

片段 工具栏中按钮单击处理程序的代码片段

private function OnAttackButtonClickListener(event : MouseEvent) : Void
{
    if (this.OnAttackButtonCallbacks != null)
    {
        var index:Int = this.OnAttackButtonCallbacks.length;
        while (index-- > 0) 
        {
            this.OnAttackButtonCallbacks[index](event);
        }
    }
}

我想知道这是否是一个好方法,或者是否有更好的方法?

I am creating a simple UI toolbar component in Haxe for use in a Flash game.
The toolbar is your general UI style toolbar, it has a background and several buttons.
When the buttons are clicked some game action happens.

I have a question about the best way to implement events for the toolbars buttons.
A parent class will create a toolbar and then sign up any functions it wants to the button on click events.

My current solution looks like this:

  • The toolbar extends the sprite class.
  • The toolbar class has a child sprite for each button.
  • Each button has an event listener for MouseEvent.Click event.
  • The MouseEvent.Click is handled by a function on the toolbar that examines a Dynamic typed array and executes each function contained in the array.
  • The Dynamic typed array is public so any classes that want to sign up to the toolbar
    button on click events can add their callback function to the array.

Code Snippet for a Scene that has a toolbar

this.toolbar = new ToolBar();
this.toolbar.OnAttackButtonClicked.push(OnAttackButtonClicked);

Code Snippet for creating a button in the toolbar

this.attackButton = new Sprite();
this.attackButton.addEventListener(MouseEvent.CLICK, this.OnAttackButtonClickListener);

Code Snippet for the button click handler in the toolbar

private function OnAttackButtonClickListener(event : MouseEvent) : Void
{
    if (this.OnAttackButtonCallbacks != null)
    {
        var index:Int = this.OnAttackButtonCallbacks.length;
        while (index-- > 0) 
        {
            this.OnAttackButtonCallbacks[index](event);
        }
    }
}

I am wondering if this is a good approach or if there is a better way to do it?

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

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

发布评论

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

评论(1

妳是的陽光 2024-08-05 06:57:37

解决了。
使用自定义事件和事件调度程序。

Solved.
Used Custom Events and the Event Dispatcher.

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