There are some parts of the code which you would want to run in the android version but not in the desktop version of your game/app like adding ads, integrating scoring libraries like (OpenFeint, Scoreloop etc). It really simple to do that also. There is a post in libgdx project page for how to integrate admob in the application. Integrating admob is done in the same manner in our game also while for adding Scoreloop we followed a pattern similar to that explained in the post.
Adding Android Specific code:
First we make an interface which would have the functions that are required to run only in the android environment. Lets make a simple interface.
public interface AndroidOnlyInterace{ public void Hello(); }
Now we would implement this in two classes one would be AndroidGame class in the Android project and other in DesktopGame , the class in the Desktop project.
Here is the GameAndroid class implementation.
public class AndroidGame extends AndroidApplication implements AndroidOnlyInterface{ ... @Override public void Hello(){ Gdx.app.log("AndroidGame","Hello Android"); } }
Here is the implementation for DesktopGame class.
public class DesktopGame implements AndroidOnlyInterface{ private static DesktopGame application; public static void main(String[] args) { if(application==null) application=new DesktopGame(); new JoglApplication(new Game(application), "Game", 320, 480, false); } @Override public void Hello(){ Gdx.app.log("GameDesktop","Hello Desktop"); } }
On running in desktop you will see the message “Hello Desktop” in logcat while in android you will see “Hello Android”. So in this way we can implement android specific code.
Here 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 specific code.
public class Game implements ApplicationListener,InputProcessor { private Boolean _isInitialized=false; Screen _currentScreen; OrthographicCamera camera; TapResponse _tapResponse; Vector3 touchPoint; AndroidOnlyInterface aInterface; public Game(AndroidOnlyInterface aInterface){ this.aInterface=aInterface; } @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; //This will print "Hello Desktop" in Desktop code and "Hello Android" in Android code. aInterface.Hello(); } } @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 }
Next Tutorial:
In the next tutorial we will integrate scoreloop in our game for global scoreboard.
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.
In the post you say:
On running in desktop you will see the message “Hello Desktop” in logcat while in android you will see “Hello Android”
But you don’t explain how the function Hello() is called from the main platform independant code?
Hi,
I added a snippet at the end to show how to call Hello() function . Hope it helps.
Thanks