[LibGDX]Making a game work for desktop AND android?

My game is using Slick2D and I am considering switching to LibGDX.

One thing that I dont get is how the hell is a game supposed to work for desktop AND android?

For example, when playing on desktop, I use my keyboard to move around. Thus, my game is listening for keystrokes.

When playing on android, I have to add buttons to the screen which we will use instead. This is a totally different behavior.

You have to write the input for each plattform yourself,
but you should opimize the game for one plattform :slight_smile:

Yeah that is an issue. You may want to make two seperate versions: one for Desktop, one for Android, that share a lot of the common code but differ in user interface code. In eXo I tried to get around this by designing the UI in such a way that its both useable on a touchscreen and on a PC (e.g. big buttons, only left mouse clicks). On the PC, in addition, I have added some keyboard shortcuts to make maximum use of the PC user interface.

One other useful gadget to use for some games is an on-screen touchpad on Android, that works similar to keyboard input on PC:
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/Touchpad.html

I do not know. Depends on your game. Not every game is suitable for touch screen controls. And not every game is suitable for keyboard or mouse or gamepad.

Imho, the virtual on-screen pad is the worst controller ever created. Do not use it.

In code, using different controls in different platforms is very simple, you just compare which one you want to use, like:



if (Gdx.app.getType() == ApplicationType.Android) {
   handleAndroidControls();
} else if (Gdx.app.getType() == ApplicationType.Desktop) {
   handleDesktopControls();
}

I made an onscreen keypad for my first game.

This is how you can read for screen touches in LibGDX…


			if (Gdx.input.justTouched()) {
				if (Gdx.input.getX() > 400 && Gdx.input.getX() < 500 && Gdx.input.getY() > 800 && Gdx.input.getY() < 900) {
					//Code for the above "touch-spot"
				}
				if (Gdx.input.getX() > 600 && Gdx.input.getX() < 700 && Gdx.input.getY() > 800 && Gdx.input.getY() < 900) {
					//Code for the above "touch-spot"
				}
				//Maybe some more "touch-spots"
			}

I suggest adding another block of code when adding in input controls for your Android version, that way you can delete out the unnecessary code when your finished.

Start learning LibGDX for a couple of days and then you will realize that with a couple of “if” statements you can make the input work in any platform.

Do not use pooling for the input work, use the callbacks, this way you can avoid all the ifs to control the different platforms.

I use the same input logic for all platforms. I just map the platform input into game input. That way each new platform gets a mapping class based not just on platform but platform capabilities. Like Ouya has analog controls but not keyboard, mouse, orientation, or touch yet it is still Android.

This is from a book…

 
switch (Gdx.app.getType()) { case Desktop:
}
// Code for Desktop application break;
case Android:
// Code for Android application break;
case WebGL:
// Code for WebGL application break; default: // Unhandled (new?) platform application break