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.

Game:

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

public class Assets{
   //...................
   //VIEWPORT SIZE
   public static final int VIRTUAL_WIDTH=320;
   public static final int VIRTUAL_HEIGHT=480;
   public static final int PIXEL_DENSITY=16;
//...................
}

ScreenSettings

public class ScreenSettings{
   //....................
   //IDS FOR DIFFERENT SCREENS
   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
   }
   @Override
   public void create() {
     if(!_isInitialized){
     //Initialize MenuScreen
     camera=CameraHelper.createCamera2(ViewportMode.STRETCH_TO_ASPECT, Assets.VIRTUAL_WIDTH,
     Assets.VIRTUAL_HEIGHT,Assets.PIXEL_DENSITY);
     _currentScreen=new MainMenu(ScreenSettings.SCREEN_NONE,camera,_tapResponse);
     _isInitialized=true;
     }
   }
   @Override
   public void render() {
    // TODO Auto-generated method stub
    float dt=Gdx.graphics.getDeltaTime();
    Update(dt);
   }
   public void Update(float dt){
     UpdateScreen();
     UpdateTouch();
   }
   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){
       touchPoint.set(x,y,0);
       camera.unproject(touchPoint);
       _tapResponse.UpdateTouch((int)touchPoint.x,
       (int)touchPoint.y,Gdx.input.isTouched());
     }
   }
   public void UpdateScreen(){
     Application app = Gdx.app;
     _currentScreen.update(app);
     _currentScreen.render(app);
     if(_currentScreen.isDone()){
     int nextScreenID=_currentScreen.BackScreenID;                 //Get the screen which called the current screen
     _currentScreen.dispose();        //Disposing the resources of current screen
     if(nextScreenID==ScreenSettings.SCREEN_NONE){
       ExitApplication();
       return;
     }
     if(nextScreenID==ScreenSettings.MAINMENU_SCREEN){
       _currentScreen=new MainMenu(ScreenSettings.SCREEN_NONE,camera,_tapResponse);
       return;
     }
     if(nextScreenID==ScreenSettings.GAMEPLAY_SCREEN){
       //INSTANTIATE GAMEPLAY SCREEN
       return;
      }
     //Handle other screens in similar way
    }
   }
   void ExitApplication(){
      Gdx.app.exit(); //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
}

MainMenu:

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){
       BackScreenID=backScreenid;
       camera=cam;
       tapResponse=tapresponse;
       spriteBatch=new SpriteBatch();
       spriteBatch.setProjectionMatrix(camera.combined);
       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
   }
   @Override
   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
   }
   @Override
   public void render (Application app){
      Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT);  //Clear the screen
      spriteBatch.begin();
      spriteBatch.disableBlending();
      //Render those textures which dont have any alpha/transparent component
      //As it is faster to draw them while blending is disabled
      spriteBatch.enableBlending();
      spriteBatch.end();
    }
    @Override
    public void dispose (){
       //Dispose of the textures,fonts
       //dispose of spritebatch
    } 
    @Override
    public void OnPause(){
    }
    @Override 
    public void OnResume(){
    } 
    @Override
    public boolean isDone() {
       return IsDone;
    }
}

Summary:

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.