Game Dev Tutorial II: Import Statements, Classes, Texture
Game Dev -Next - Tutorial III
This tutorial will continue on from our first tutorial, and discuss the same game screen code. We are going to learn more about 'import statements', 'spritebatch'(for adding a texture), 'adding game variables', 'create method', 'render method', and how to dispose of the game assets. And, we
discuss the screen classes that can be used in making a mobile game. There are several you can implement; for this tutorial, we will learn about the 'Applicaton Listener' class. You can view the game screen code(MyGdxGame.java) in the Example Code section on this page, and from the images shown in this tutorial.
App Shot - MyGame
Import Statements
The first code you see in the
MyGdxGame.java screen code is the import statements.
The
import statements, as mentioned in our first tutorial are really important. You must add the appropriate import statement for the code method you are adding to code. If you do not add the import statement or the wrong one you will
get an error when you try to run your game code. Most likely an 'unknown entity' error message will populate. I have compiled a list of common import statements for game dev in this link which you can use. You can also copy and paste the list,
Import Statements,
into your own editor to save on your device.
For this game screen, we've added three import statements required to run the code for this app we created. Like most imports the name is easily relatable to the code method we are adding. The 'graphics import' is required because we are adding a 'texture'(an image) and the 'spritebatch'(a method) in code to draw it. It does not matter which order you list the imports, or when, just add them before running your code to compile your code.(make the app)
Application Listener
After the imports code is the
'Application Listener', line of code.
public class MyGdxGame implements ApplicationListener
The Applicaton Listener is the most important code on your screen. It is a
class that has the core components required to make every method you call in your game code to work.(execute) Usually, this class is added as the first game screen, and your other game screens(assuming you have more than 1 screen for your game), are then added additionally.
The
MyGdxGame is just a name that was used to name this screen; you can name it as you like. The
'implements Application Listener' must be added exactly as shown because this represents a class name and how it gets added. ApplicationListener is a game class. Class meaning it is a java type of code, so the screen name created using this class method must end with
.java like the:
MyGdxGame.java screen. In gamedev, there are several screen classes you can use, but Application Listener is the core class you must add first. Other game screen classes include: game, actor, screen. We shall learn about these additional game classes as we continue our tutorials.
Add Code for Game Variables
The next code we add is for our game(screen) variables for this screen;
Texture texture;
SpriteBatch batch;
To add our image we must use the
Texture method, and the variable for that is added here as shown; then we must add the
Spritebatch, method, because it is required to add(draw) the texture, so we must add it here also. Anytime we add an asset to our game, we must first declare what it is we are going to add and this section in code is where we do that.
Create Method, Render Method
After we declare our game variables,
we must then add the 'create' and 'render' methods to our code. In this case, we want to add our texture to the screen. So in the
Create Method , the image info required is added. In gamedev an image is known as a texture. Here we add the 'new Texture' method to add our image location, which is in the 'Assets' folder, in our file hierachy. But the Libgdx knows where to get it, we just need to add the code as shown here then it can get the image; 'android.jpg'. So the code, Gdx.
files.internal("android.jpg")), tells LibGDX to goto the 'assets' folder to get the image and its' name. If you remember from our first tutorial, the 'assets' folder is at:
gdx-game-android, folder, in the AppProjects/AppName/ folder hierachy.
public void create()
{
texture = new Texture(Gdx.files.internal("android.jpg"));
batch = new SpriteBatch();
In addition to adding the
texture = new 'Texture(Gdx.files.internal("android.jpg");' method, we also include the
'batch = new SpriteBatch();' code method. The SpriteBatch(); method is what draws the texture to the screen. So, anytime you add a texture, you should add a SpriteBatch also.
The
render method is used to actually begin the batch - the SpriteBatch; and then end the batch process. So it begins, draws the image to screen and then the
batch.end();, ends the draw; as shown in this code snippet.
batch.begin();
batch.draw(texture, Gdx.graphics.getWidth() / 4, 0,
batch.end();
Set Texture Position on Screen
Also in the render code section, we set the position of our texture on screen with this code:
Gdx.graphics.getWidth() / 4, 0, Gdx.graphics.getWidth() / 2, Gdx.graphics.getWidth() / 2
This
is a method call in the libGDX game dev framework. This is the default code in our template game app, so any image we add will position itself according to this code. However we can change it. With this code method we draw a rectangle on the screen. The first parameter specifies the x-coordinate of the bottom-left corner of the rectangle, the second parameter specifies the y-coordinate of the bottom-left corner of the rectangle, and the third and fourth parameters specify the width and height of the rectangle respectively. More on positioning textures in another tutorial.
Screen Color
The render method is also where you add your screen color, assuming you want a particular color background.
This line of code is your screen color; it can be any color you like, this represents a white color background:
1,1,1,1. in this line of code:
Gdx.gl.glClearColor(1, 1, 1, 1);
This next line of code is for a yellow background screen.
You can use single numbers or rgb color code numbers like shown here.
Gdx.gl.glClearColor(255/255f, 241/255f, 118/255f, 1);
The code itself:
Gdx.gl.glClearColor(1,1,1,1), means, to clear the present screen and then add the new color as shown, (1,1,1,1)(white).
Dispose, Resize, Pause, Resume - Game States
In GameDev you have game states, which serve to adjust the state of the game when certain things happen in the game. Like for example, the game is paused by the user(pause state), and then they begin game play again(resume play state). And, perhaps they go from landscape to portrait mode while playing your game(resize game state). All these states must be in the game code or all the different game states could not be achieved.
The 'dispose' state is important also; its' job is to get rid of images and assets your game no longer is currently using. This frees up game play space making your game perform much better giving a better user experience. If you don't dispose of your assets after each use, game play can begin to lag, especially in a larger mobile game that has lots of images and assets.
In Summary
With LibGDX, you can implement many methods to add game assets like textures. The games' first screen should have the 'ApplicationListener' class, as this includes the base methods required to start your game and activate the various states it would need. From the ApplicationListener class, you can then add the other game classes like: game, screen, actor, table. All these classes allow you to multi level your game and add more functionality to it.
Unlike a regular Android app, game apps only have one activity. The various screens you add in your game are implemented with LibGDX, not as separate activities. If each new class you added was a new activity, this would take more time to load, thus slowing your game performance. With LibGDX implementing all the classes, the game runs smoother and more efficient.
For each screen you code, you should add(declare) your variables, then add your create and render methods. Also, dispose of your assets to keep your game at better performance. And, add the appropriate 'import statements' to code for the methods you add.
Additional Reading:
LibGDX - Classes and Configuration