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
+6-4Lines changed: 6 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -118,7 +118,7 @@ The key changes here are:
118
118
With the scene architecture in place, the game can now be updated so that it is broken down into scenes. We will create two scenes; a title scene and a gameplay scene. First, however, we need to add an additional SpriteFont Description that will be used during the title scene to display the title of the game. Open the *Content.mgcb* content project file in the MGCB Editor and perform the following:
119
119
120
120
1. Right-click the `fonts` folder and choose `Add > New Item...`.
121
-
2. Select `SpriteFont Description (.spritefont)` from the options
121
+
2. Select `SpriteFont Description (.spritefont)` from the options.
122
122
3. Name the file `04B_30_5x` and click `Create`.
123
123
124
124
||
@@ -134,7 +134,7 @@ Next, open the *04B_30_5x.spritefont* file in your code editor and make the foll
134
134
The title scene serves as the game's initial starting point; the first impression the player gets when they launch the game. For our game, the title scene will display the text for the title of the game and a prompt to inform the player what action to take to start the game. We will use a simple trick for the title text in order to draw it with a drop shadow to add a bit of visual flair.
135
135
136
136
> [!NOTE]
137
-
> As the following screens are specific to our game and are not reuable bits, these will be added to your game project.
137
+
> As the following screens are specific to our game and are not reusable bits, these will be added to your game project.
138
138
>
139
139
> Although, if you do end up making screens that are completely reusable, there is nothing wrong with putting them in your Game Library, it is completely up to you.
140
140
@@ -320,9 +320,11 @@ With our scene system and scene classes in place, we can now simplify our main `
320
320
[!code-csharp[](./snippets/game1.cs)]
321
321
322
322
> [!NOTE]
323
-
> Feel free to check your homework and compare the original `Game1` class with the updated version, as well as checking the `GameScene` class did not lose any functionality (it has not but you have to be sure!). Refactoring code to be cleaner and more organised is a careful task.
323
+
> Feel free to check your homework and compare the original `Game1` class with the updated version, as well as checking the `GameScene` class did not lose any functionality (it has not, but you have to be sure!). Refactoring code to be cleaner and more organised is a careful task.
324
324
325
-
The `Game1` class is now much simpler as most of the game logic has been moved to the appropriate scene classes. It:
325
+
The `Game1` class is now much simpler as most of the game logic has been moved to the appropriate scene classes.
326
+
327
+
The updates include:
326
328
327
329
1. Sets up the game window with the constructor parameters.
328
330
2. Overrides the `Initialize` method to set the title scene as the starting scene.
Copy file name to clipboardExpand all lines: articles/tutorials/building_2d_games/20_implementing_ui_with_gum/index.md
+39-21Lines changed: 39 additions & 21 deletions
Original file line number
Diff line number
Diff line change
@@ -14,15 +14,15 @@ In this chapter you will:
14
14
* Handle input from keyboard, mouse, and gamepads
15
15
* Integrate the UI system with our existing game architecture.
16
16
17
+
> [!IMPORTANT]
18
+
> While GUM is used in this tutorial it is only one of many UI libraries available to the MonoGame community, some notable others are [EmptyKeys](https://github.com/EmptyKeys/UI_Engines), [GeonBit.UI](https://github.com/RonenNess/GeonBit.UI), as well as entire Game Frameworks/Engines like [Nez}(https://github.com/prime31/Nez) that have their own built in UI systems.
19
+
>
20
+
> Check out the [MonoGame Resources](https://monogame.net/resources/) page, as well as [awesome-monogame](https://github.com/aloisdeniel/awesome-monogame) from [Alo�s Deniel](https://github.com/aloisdeniel) for even more community offerings.
21
+
17
22
## What is Gum?
18
23
19
24
Gum is a powerful UI layout engine and framework. It provides a flexible, efficient system capable of producing virtually any UI layout you might need in your games. While originally developed alongside the FlatRedBall game engine, Gum has evolved to work seamlessly with multiple platforms, including MonoGame, which we will be using in this tutorial.
20
25
21
-
> [!IMPORTANT]
22
-
> While GUM is used in this tutorial it is only one of many UI libraries available to the MonoGame community, some notable others are [EmptyKeys ](https://github.com/EmptyKeys/UI_Engines), [GeonBit.UI](https://github.com/RonenNess/GeonBit.UI), as well as entire Game Frameworks/Engines like [Nez}() that have their own built in UI systems.
23
-
>
24
-
> Check out [awesome-monogame](https://github.com/aloisdeniel/awesome-monogame) from [Aloïs Deniel](https://github.com/aloisdeniel) as well as the [MonoGame Resources](https://monogame.net/resources/) page for even more community offerings.
25
-
26
26
### Why Use Gum?
27
27
28
28
Creating a UI system from scratch requires solving many complex problems:
@@ -360,7 +360,7 @@ To make our UI more responsive and engaging, we will add audio feedback that pla
360
360
361
361
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:
362
362
363
-
* [ui.wav](./files/ui.wav)
363
+
* [ui.wav](./files/ui.wav){download}
364
364
365
365
Next, add this sound effect to your content project using the MGCB Editor:
366
366
@@ -422,11 +422,14 @@ Gum is now fully initialized and we can use it in our scenes to add UI to our ga
422
422
423
423
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.
424
424
425
-
First, open the `TitleScene.cs` file in the game project and add the following using declarations to the top of the `TitleScene` class:
425
+
> [!NOTE]
426
+
> 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.
427
+
428
+
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:
430
433
431
434
[!code-csharp[](./snippets/titlescene/fields.cs)]
432
435
@@ -463,7 +466,7 @@ Add the following method to the `TitleScene` class:
463
466
464
467
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.
465
468
466
-
Now we should implement the event handlers for these controls:
469
+
Now we should implement the event handlers for these controls. Add the following methods to the `TitleScene` class after the `CreateOptionsPanel` method:
@@ -482,23 +485,23 @@ These handlers update our audio settings in real-time as the player adjusts the
482
485
483
486
#### Initializing the UI
484
487
485
-
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:
488
+
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.
490
493
491
494
#### Integrating with the Game Loop
492
495
493
-
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:
496
+
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:
@@ -524,25 +527,28 @@ With these changes, our UI system is now fully integrated into the scene's game
524
527
525
528
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.
526
529
530
+
> [!NOTE]
531
+
> 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.
532
+
527
533
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:
532
538
533
539
[!code-csharp[](./snippets/gamescene/fields.cs)]
534
540
535
541
#### Pausing the Game
536
542
537
-
To pause the game, first we will create a method that makes the pause panel visible. Add the following method to the `GameScene` class:
543
+
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:
Now we should implement the event handlers for these controls. First, we will implement the handler for the "Resume" button. Add the following method to the `GameScene` class after the `CreatePausePanel` method:
This method as well plays the UI sound effect for auditory feedback, then quits the game by changing scenes back to the title scene.
572
+
555
573
#### Initializing the Game UI
556
574
557
-
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:
575
+
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.
562
580
563
581
#### Integrating with the Game Loop for the GameScreen
564
582
565
-
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:
583
+
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, add the following to the beginning of the `Update` method to include Gum's update logic and to only update the game if it is not paused, pausing will prevent any other Game update logic running. We will use the visibility of the pause menu to determine if the game is paused or not:
591
+
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.
599
+
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.
582
600
583
601
| |
0 commit comments