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/17_scenes/index.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -294,7 +294,7 @@ Add the following override for the `Draw` method to the `GameScene` class:
294
294
295
295
### Updating the Game1 Class
296
296
297
-
With our scene system and scene classes in place, we can now simplify our main `Game1` class to just initialize the game and start with the title scene. Open the *Game1.cs* file and update it to the following:
297
+
With our scene system and scene classes in place, we can now simplify our main `Game1` class to just initialize the game and start with the title scene. Open the *Game1.cs* file and replace its entire contents with this simplified implementation:
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/20_implementing_ui_with_gum/index.md
+25-19Lines changed: 25 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -342,7 +342,7 @@ To make our UI more responsive and engaging, we will add audio feedback that pla
342
342
343
343
First, download the UI sound effect by right-clicking the following link and saving it as *ui.wav*in the game project's *Content/audio* folder:
344
344
345
-
- [ui.wav](./files/ui.wav)
345
+
- [ui.wav](./files/ui.wav){download}
346
346
347
347
Next, add this sound effect to your content project using the MGCB Editor:
348
348
@@ -404,17 +404,20 @@ Gum is now fully initialized and we can use it in our scenes to add UI to our ga
404
404
405
405
With Gum added and initialized in our game, we can now implement UI elements for our title scene. We will create panels for both the main menu and options menu, implement the necessary event handlers, and integrate everything with our existing title scene.
406
406
407
+
> [!NOTE]
408
+
> When adding these sections one by one, you may see compiler errors until all sections are in place. This is normal, as some parts of the code will reference fields or methods that haven't been added yet. Once all sections are complete, these errors will resolve.
409
+
407
410
First, open the *TitleScene.cs* file in the game project and add the following using declarations to the top of the `TitleScene` class:
Next, add the following fields to the `TitleScene` class:
412
415
413
416
[!code-csharp[](./snippets/titlescene/fields.cs)]
414
417
415
418
#### Creating the Title Panel
416
419
417
-
First, wew ill create a method that builds our main menu panel with start and options buttons. Add the following method to the `TitleScene` class:
420
+
First, wew ill create a method that builds our main menu panel with start and options buttons. Add the following method to the `TitleScene` class after the fields:
@@ -423,7 +426,7 @@ Our title panel includes two buttons positioned at the bottom corners of the scr
423
426
> [!NOTE]
424
427
> 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.
425
428
426
-
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. Add the following methods to the `TitleScene` class:
429
+
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. Add the following methods to the `TitleScene` class after the `CreateTitlePanel` method:
@@ -433,13 +436,13 @@ These handlers are called when the `Click` event is raised for each button. The
433
436
434
437
#### Creating the Options Panel
435
438
436
-
Next, we will create the options panel with sliders to adjust the volume for music and sound effects. Add the following method to the `TitleScene` class:
439
+
Next, we will create the options panel with sliders to adjust the volume for music and sound effects. Add the following method to the `TitleScene` class after the `HandleOptionsClicked` method:
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.
441
444
442
-
Now we should implement the event handlers for these controls:
445
+
Now we should implement the event handlers for these controls. Add the following methods to the `TitleScene` class after the `CreateOptionsPanel` method:
@@ -458,23 +461,23 @@ These handlers update our audio settings in real-time as the player adjusts the
458
461
459
462
#### Initializing the UI
460
463
461
-
Now that we have implemented the methods that will create both the main menu panel and the options menu panel, we need to implement the main UI initializations method that will call them. Add the following method to the `TitleScene` class:
464
+
Now that we have implemented the methods that will create both the main menu panel and the options menu panel, we need to implement the main UI initializations method that will call them. Add the following method to the `TitleScene` class after the `HandleOptionsButtonBack` method:
This method first clears any existing UI elements from Gum's root container to prevent duplication, then calls our panel creation methods to build the complete interface.
466
469
467
470
#### Integrating with the Game Loop
468
471
469
-
Finally, we need to integrate our UI initialization, update, and draw with the scene's lifecycle. First, add the call to `InitializeUI()` in the `Initialize` method:
472
+
Finally, we need to integrate our UI initialization, update, and draw with the scene's lifecycle. First, add the call to `InitializeUI()`in the `Initialize` method by updating it to the following:
@@ -500,25 +503,28 @@ With these changes, our UI system is now fully integrated into the scene's game
500
503
501
504
Now that we have setup the UI forthe title scene, we will add a pause menu to our game scene. This UI will start invisible but will be shown when the player presses the escape key. For consistency, we will implement the UI for the game scenein the same order that we implemented the UI for the title scene.
502
505
506
+
> [!NOTE]
507
+
> When adding these sections one by one, you may see compiler errors until all sections are in place. This is normal, as some parts of the code will reference fields or methods that haven't been added yet. Once all sections are complete, these errors will resolve.
508
+
503
509
First, open the *GameScene.cs* file in the game project and add the following using declarations to the top of the `GameScene` class.
Next, add the following fields to the `GameScene` class:
508
514
509
515
[!code-csharp[](./snippets/gamescene/fields.cs)]
510
516
511
517
#### Pausing the Game
512
518
513
-
To pause the game, first we will create a method that makes the pause panel visible. Add the following method to the `GameScene` class:
519
+
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:
@@ -530,31 +536,31 @@ Next, we will create a method that builds our pause panel with resume and quit b
530
536
531
537
#### Initializing the UI
532
538
533
-
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:
539
+
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:
Just like with the `TitleScene`, we first clear any existing UI elements from Gum's root before creating the UI elements for this scene.
538
544
539
545
#### Integrating with the Game Loop
540
546
541
-
Finally, we need to integrate our UI initialization, update, and draw with the scene's lifecycle. First add the call to `InitializeUI()` in the `Initialize` method:
547
+
Finally, we need to integrate our UI initialization, update, and draw with the scene's lifecycle. First add the call to `InitializeUI()` in the `Initialize` method by updating it to the following:
Next, update the `Update` method to include Gum's update logic and to only update the game if it is not paused. We will use the visibility of the pause menu to determine if the game is paused or not:
555
+
Next, modify the `Update` method to include Gum's update logic and to only update the game if it is not paused. We will use the visibility of the pause menu to determine if the game is paused or not:
WIth these changes, the pause menu is now fully integrated into the game scene's game loop. Gum updates its controls during the `Update` method and draws them during the `Draw` method. If the game is paused, as determined by the `IsVisible` property of the pause menu, then updating the actual game logic is skipped.
563
+
With these changes, the pause menu is now fully integrated into the game scene's game loop. Gum updates its controls during the `Update` method and draws them during the `Draw` method. If the game is paused, as determined by the `IsVisible` property of the pause menu, then updating the actual game logic is skipped.
558
564
559
565
| |
0 commit comments