Note that there are some explanatory texts on larger screens.

plurals
  1. POEvent Handling For Toolbar Using Haxe Targeting Flash
    text
    copied!<p>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.</p> <p>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.</p> <p>My current solution looks like this:</p> <ul> <li>The toolbar extends the sprite class.</li> <li>The toolbar class has a child sprite for each button.</li> <li>Each button has an event listener for MouseEvent.Click event.</li> <li>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.</li> <li>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.</li> </ul> <p>Code Snippet for a Scene that has a toolbar</p> <pre><code>this.toolbar = new ToolBar(); this.toolbar.OnAttackButtonClicked.push(OnAttackButtonClicked); </code></pre> <p>Code Snippet for creating a button in the toolbar</p> <pre><code>this.attackButton = new Sprite(); this.attackButton.addEventListener(MouseEvent.CLICK, this.OnAttackButtonClickListener); </code></pre> <p>Code Snippet for the button click handler in the toolbar</p> <pre><code>private function OnAttackButtonClickListener(event : MouseEvent) : Void { if (this.OnAttackButtonCallbacks != null) { var index:Int = this.OnAttackButtonCallbacks.length; while (index-- &gt; 0) { this.OnAttackButtonCallbacks[index](event); } } } </code></pre> <p>I am wondering if this is a good approach or if there is a better way to do it?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload