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.
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
In our post Game Development in Android using Libgdx Part I ( http://decisiontreegames.wordpress.com/2012/01/30/game-development-in-android-using-libgdx-part-ii/) we have created our own abstract Screen class and then extended it.
Thanks…. I really appreciate what you are doing … wish you all the best 😀
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?
Hi,
You can download example code in the post (http://rotatingcanvas.com/box2dlibgdx-example-code/ ). It has camera setup.
I believe there is a typo in this code. spritebatch should be spriteBatch
The variable that is. There is also a spot where you create a new SpriteBatch, but you have spriteBatch there. These are both in MainMenu
Thanks for pointing out the typo. Fixed it. The var should be spriteBatch.