How To Make Android Games. Part 3.

In Part 3 we look inside the class that implements an ApplicationListener interface. This is a special interface in which certain methods are called at certain points.

How To Make Android Games Part 3

Previously we created three projects in Eclipse (IDE): test-game, test-game-android, test-game-desktop. "test-game" is the main project. It contains the source code of our game. "test-game-desktop" and "test-game-android" provide us witrh the opportunity to run this code on the target platforms. The codes in the projects for android and desktop are very simple, and usually consist of single files. For example, let's look at the code of "test-game-desktop":

package com.testgame; import com.badlogic.gdx.backends.lwjgl.LwjglApplication; import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration; public class Main { public static void main(String[] args) { LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration(); cfg.title = "test-game"; cfg.useGL20 = false; cfg.width = 480; cfg.height = 320; new LwjglApplication(new MyTestGame(), cfg); } }
Now let's look at the code of "test-game-android":

package com.testgame; import android.os.Bundle; import; import; public class MainActivity extends AndroidApplication { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration(); cfg.useGL20 = false; initialize(new MyTestGame(), cfg); } }
But the most important is the "test-game" code, because this is where we classify actions. And now let's look inside the class that implements the ApplicationListener interface where we describe every aspect of it.

package com.testgame; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; import; import; import; import; import; import; import; //Class title. Please note that it implements the ApplicationListener interface. //Your game should have at least one class that implements this interface. //Later we will explain the meaning of it. public class MyTestGame implements ApplicationListener { //Virtual camera through which we "see" our game private OrthographicCamera camera; //Sprite renderer. This sort of thing can draw pictures of the right size and in the right places //We will use it frequently private SpriteBatch batch; //Texture. The base for the graphic part of the game. //Your game can include many textures private Texture texture; //Sprite. It is able to visualize itself, and has a certain position, size, and other parameters private Sprite sprite; //Method that initializes our game. This method calls the main task - //prepares necessary resources @Override public void create() { //The width and height of the screen - screen resolution. float w =; float h =; //Camera and sprite initializing. camera = new OrthographicCamera(1, h/w); batch = new SpriteBatch(); //Loading texture, enables bilinear filtering. texture = new Texture(Gdx.files.internal("data/libgdx.png")); texture.setFilter(TextureFilter.Linear, TextureFilter.Linear); //Part of a texture called - texture region TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275); //Sprite resolution is set almost through the entire screen sprite = new Sprite(region); sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth()); sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2); sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2); } //This method clears the resources after the game is closed @Override public void dispose() { batch.dispose(); texture.dispose(); } //Method for every frame rendering. FPS may be unstable. @Override public void render() { //Clearing the screen - OpenGL functions, 1, 1, 1);; //Telling sprites to move inside the camera's coordinate system batch.setProjectionMatrix(camera.combined); //We call this method first... batch.begin(); //...then specify what to visualize sprite.draw(batch); //...and at the end - final rendering batch.end(); } //This method is called when we resize the window - used only in desktop mode @Override public void resize(int width, int height) { } //This method is called when we close a phone application or we receive a call. @Override public void pause() { } //If we run application that is already running then this method is called. @Override public void resume() { } }
In our next tutorial we will look inside such important classes as SpriteBatch, Texture and TextureRegion.
You can find Part 2 of this tutorial here.

What do you think about How to Make Android Games. Part 3.? Please, leave your comments in the section below.

Share this article with your friends:

No comments:

Post a Comment