You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/20_implementing_ui_with_gum/index.md
+27-9Lines changed: 27 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -447,15 +447,17 @@ Our title panel includes two buttons positioned at the bottom corners of the scr
447
447
> [!NOTE]
448
448
> Notice how we use `Anchor` to position the buttons relative to the panel's edges, with the "Start" button anchored at the bottom left and the "Options" button anchored at the bottom right. Then the positioning of the elements is adjusted relative to its anchor point.
449
449
450
-
Each button registers a `Click` event handler to respond when the players selects it, we should implement the event handler method for these buttons next.
451
-
452
-
Add the following methods to the `TitleScene` class:
450
+
Each button registers a `Click` event handler to respond when the players selects it, we should implement the event handler method for these buttons next. First we will implement the handler for the "Start" button. Add the following method to the `TitleScene` class after the `CreateTitlePanel` method:
When the "Start" button is clicked and this method is called, it will play the UI sound effect for auditory feedback then change the scene tot he game scene so the player can start playing the game.
455
+
456
+
Next is the handler for the "Options" button. Add the following method to the `TitleScene` class after the `HandleStartClicked` method:
These handlers are called when the `Click` event is raised for each button. The handler for the "Start" button changes to the game scene, while the handler for the options button toggles the visibility between the main menu and the options panel.
460
+
When the "Options" button is clicked and this method is called, it will play the UI sound effect for auditory feedback then hide the title panel and show the options panel.
459
461
460
462
#### Creating the Options Panel
461
463
@@ -467,19 +469,35 @@ Add the following method to the `TitleScene` class:
467
469
468
470
This panel includes a text label, two sliders for adjusting audio volumes, and a back button for returning to the main menu. The panel is initially invisible since we start on the main menu. Both the "Music Volume" slider and the "Sound Effects Volume" slider register events to be called when the value of the sliders change and when the value change has been completed. The "Back" button registers a click event similar to the ones from the main menu.
469
471
470
-
Now we should implement the event handlers for these controls. Add the following methods to the `TitleScene` class after the `CreateOptionsPanel` method:
472
+
Now we should implement the event handlers for these controls. First, we will implement the handler for when the value of the sound effect volume slider changes. Add the following method to the `TitleScene` class after the `CreateOptionsPanel` method:
When the value of the "Sound Effects Volume" slider changes and this method is called, a reference to the slider is captured and then the the global sound effect volume is adjusted based on the value of the slider.
477
+
478
+
Next is the handler when the "Sound Effects Volume" slider has completed a value change. Add the following method to the `TitleScene` class after the `HandleSfxSliderChanged` method:
When the value of the "Sound Effects Volume" slider has completed a change and this method is called, it plays the UI sound effect to provide auditory feedback so the player can hear the difference in volume.
483
+
484
+
After this is the handler for when the "Music Volume" slider value changes. Add the following method to the `TitleScene` class after the `HandleSfxSliderChangeCompleted` method:
Similar to how we handled the "Sound Effect Volume" slider value changes, when the "Music Volume" slider value changes and this method is called, a reference to the slider is captured and then the global music volume is adjusted based on the value of the slider.
489
+
490
+
Next is the handler when the "Music Volume" slider value has completed a value change. Add the following method to the `TitleScene` class after the `HandleMusicSliderValueChanged` method:
When the value of the "Music Volume" slider has completed a change, the UI sound effect is played to provide auditory feedback.
495
+
496
+
Finally, we need to add the handler for when the "Back" button is clicked on the options panel. Add the following method to the `TitleScene` class after the `HandleMusicSliderValueChangeCompleted` method:
These handlers update our audio settings in real-time as the player adjusts the sliders.
500
+
This method plays the UI sound effect for auditory feedback, then hides the options panel and shows the title panel.
483
501
484
502
> [!TIP]
485
503
> Notice that for both sliders, we registered a method for the `ValueChangeCompleted` event. This is so we can play the UI sound effect only when the player has finished adjusting the slider value. If we had instead played the UI sound effect in the `ValueChanged` event, then the UI sound effect would trigger constantly while the slider is being adjusted if using a mouse to drag it.
@@ -541,7 +559,7 @@ Next, add the following fields to the `GameScene` class:
541
559
542
560
#### Pausing the Game
543
561
544
-
To pause the game, first we will create a method that makes the pause panel visible. Add the following method to the `GameScene` class after the fields:
562
+
To pause the game, first we will create a method that makes the pause panel visible. Add the following method to the `GameScene` class after the `CheckGamePadInput` method:
@@ -555,7 +573,7 @@ Finally, update the `CheckGamePadInput` method so that when the start button is
555
573
556
574
#### Creating the Pause Panel
557
575
558
-
Next, we will create a method that builds our pause panel with resume and quit buttons. Add the following method to the `GameScene` class:
576
+
Next, we will create a method that builds our pause panel with resume and quit buttons. Add the following method to the `GameScene` class after the `LoadContent` method:
@@ -573,7 +591,7 @@ This method as well plays the UI sound effect for auditory feedback, then quits
573
591
574
592
#### Initializing the Game UI
575
593
576
-
Now that we have implemented the method to create the pause panel, we can implement the main UI initializations method that will call them. Add the following method to the `GameScene` class after the `CreatePausePanel` method:
594
+
Now that we have implemented the method to create the pause panel, we can implement the main UI initializations method that will call them. Add the following method to the `GameScene` class after the `HandleQuitButtonClicked` method:
0 commit comments