Game Development in Android using Libgdx Part IV

In this tutorial we would be creating two classes. One, the Game class which would be instantiated by Libgdx android/desktop projects and a MainMenu class. Game class would handle the different game screens present.


Before game class we would add some values in Assets class and make one more global settings class for screen.

public class Assets{
   public static final int VIRTUAL_WIDTH=320;
   public static final int VIRTUAL_HEIGHT=480;
   public static final int PIXEL_DENSITY=16;


public class ScreenSettings{
   public static final int NO_SCREEN=-1;
   public static final int MAINMENU_SCREEN=0;
   public static final int GAMEPLAY_SCREEN=1;
   // and so on ....................

And Now the Game class

public class Game implements ApplicationListener,InputProcessor {
   private Boolean _isInitialized=false;
   Screen _currentScreen;
   OrthographicCamera camera;
   TapResponse _tapResponse;
   Vector3 touchPoint;
   public Game(){
      //We will use this later while writing android specific code
   public void create() {
     //Initialize MenuScreen
     camera=CameraHelper.createCamera2(ViewportMode.STRETCH_TO_ASPECT, Assets.VIRTUAL_WIDTH,
     _currentScreen=new MainMenu(ScreenSettings.SCREEN_NONE,camera,_tapResponse);
   public void render() {
    // TODO Auto-generated method stub
   public void Update(float dt){
   public void UpdateTouch(){
     int x=Gdx.input.getX();   //Gdx.input.getX() gets the x position of touch point
     int y=Gdx.input.getY();  //Gdx.input.getY() gets the y position of touch point
     if(_isInitialized && !_currentScreen.IsDone){
   public void UpdateScreen(){
     Application app =;
     int nextScreenID=_currentScreen.BackScreenID;                 //Get the screen which called the current screen
     _currentScreen.dispose();        //Disposing the resources of current screen
       _currentScreen=new MainMenu(ScreenSettings.SCREEN_NONE,camera,_tapResponse);
     //Handle other screens in similar way
   void ExitApplication(){; //Libgdx function to exit the application
    //Other stubs of inputprocessor would be created
   //Leave them empty as of now
    //Later on we would implement  code there to handle the back button


Finally the MainMenu class.

public class MainMenu extends Screen{
   TapResponse tapResponse;
   SpriteBatch spriteBatch;
   OrthographicCamera camera;
   Button[] buttons;
   public int BackScreenID;
   public MainMenu(int backScreenid,OrthographicCamera cam,TapResponse tapresponse){
       spriteBatch=new SpriteBatch();
       Init();   //Initialize the textures and variable states
   void Init(){
     Assets.Load(); //in Assets class we would be loading the textures and settings
     //initialize the buttons in the menu
   public void update (Application app){
      //Check which button is clicked.
      //Set BackScreenID to the screen which should be shown next
      //depending on the button click
      //and then set IsDone=true
   public void render (Application app){;  //Clear the screen
      //Render those textures which dont have any alpha/transparent component
      //As it is faster to draw them while blending is disabled
    public void dispose (){
       //Dispose of the textures,fonts
       //dispose of spritebatch
    public void OnPause(){
    public void OnResume(){
    public boolean isDone() {
       return IsDone;


So the line “initialize(new Game(), false);” in android project or the line “new JoglApplication(new Game(), “Game”, 320, 480, false); ” instantiates the Game Class. In its constructor we make an object MainMenu. In its update we check if the current Screen(MainMenu in this case) has been disposed off. If disposed then check the next screen to be instantiated. If BackScreenID from currentScreen contains a constant called no screen then game exits. In Render function we render the buttons present in the MainMenu.

Hopefully this will help you in some way in making your game.

Next Tutorial:

In the next tutorial we would start developing a simple game in the gameplay screen.

Let us know any of your doubts/suggestions; please put them in the comments section and we would try to answer them.

Thank you for the patience.

Tagged with: , , , ,
Posted in Tutorials
8 comments on “Game Development in Android using Libgdx Part IV
  1. AMin says:

    It seems That Screen is Interface … eclipse refuses to extends it but it’s okay if it is implemented … so did you mean implement instead of extends ? Thanks

  2. Han says:

    I can’t find createCamera2 function in CameraHelper Class that you wrote in previous tutorial and the ViewportMode too.
    Could you please share the source code for this Class?

  3. Jacob Barber says:

    I believe there is a typo in this code. spritebatch should be spriteBatch

2 Pings/Trackbacks for "Game Development in Android using Libgdx Part IV"
  1. [...] Games ← Game Development in Android using Libgdx Part IV [...]

  2. [...] is the snippet for Game Class where we call Hello function. It is similar to one in of the previous post. The difference is that the constructor has an argument for the interface so that we can platform [...]

Try our games

Error: Twitter did not respond. Please wait a few minutes and refresh this page.