Now we are going to show you how you are able to manage screens in this game and to create simple text features that can appear and disappear. First we are going to take our main class and extend Game which will allow us to create the base framework for some public variables and a game screen. Thus, restructure your main class (the one that is executed upon running) to the following:
1 |
package com.mygdx.adlermac; |
This class the has three parts to it: create, render, and dispose. Create is what is called upon the program first starting, and this is where we create a Spritebatch and gameFont that can be used by any screen that we may have currently running. We also call upon the first screen to start the game, the main menu screen. As of right now the MainMenuScreen will be an error as we have not made it yet! Next section is the render block which is called every time the program comes back around to be rendered. In the block we put a simply println command so that we may see it being rendered. Finally, dispose is when the program is disposed of and were we also dispose of our Spritebatch and gameFont.
Now we need to create a MainMenuScreeen so that we may test our program. To do that, simply right-click on the same package the existing game class is in, and click create new java class. We will have to name it MainMenuScreen so that our existing class may call upon it. Once created, reformat the class to the following:
1 |
package com.mygdx.adlermac; |
This is the default layout for classes that implement Screen. As for right now we will leave the rest of this class blank.
Now you should be able to run your program and achieve something of the sort below:
It is a blank screen, but none-the-less you should be able to see our little “I have been rendered!” appear over and over again “run” dialogue box. Now lets perform a little experiment. You may have noticed that both our Game class and MainMenuScreen class have render() methods. Lets just test them both out to see what happens. Thus in our main Game class, lets change the render() method to the following:
1 |
@Override |
And then lets change the render() method in the MainMenuScreen to the following:
1 |
@Override |
Run the program and see what results….. Surprising huh? It seems our Game class keeps becoming rendered but our MainMenuScreen class does not. The reason for this is the render() method in MainMenuScreen is not called by default and we must call it manually. To do this, we simply put a snippet of code in the Game Class under its render() command, telling us to call all of the other render() methods in our program. We can do this with the following code:
1 |
//Called upon render |
Upon inserting this code.. lets run and see whats happens…….. Success! We have called the render in another class. Now there is something I have been ignoring, that you might be curios about it. The render() method in the MainMenuScreen is actually more of a render(float deltaTime) method. This deltaTime is actually a very important thing to games, it essentially sends the time in seconds that have passed between renders. To show this, we will change the render() method in the MainMenuScreen to the following (you can now remove the “Render 1” test from the Game Class, we understand that it works now):
1 |
@Override |
And this should show you a result that looks like such:
You can now get a grasp on how fast the game renders and the kind of FPS your game will run at, an important statistics, especially to the gaming communities. This most likely sums up how we are able to create a different screen and how the different kinds of render methods work so that your are able to understand more intuitively what is going on within the program.