Question

I am currently just started working with Libgdx. I reached a point where I would like to consider cross-platform (PC/handheld) issues. One important issue I can't seem to find a way around it is the following: I don't want each controllable entity to be an extension of InputAdapter, and then contain if(PC){switch: case key: increaseSpeed(); } else if(handheld) {switch: case touch: increaseSpeed();}

Is there a to implement something so that the entities would just receive the Program-related meanings of user-inputs?

Sorry if my question is too vague :' (

Was it helpful?

Solution

Generally Libgdx apps just handle all the inputs, and don't worry about which device they're on. Like this:

...

@Override
public boolean keyDown(int keycode) {
    if (keycode == Keys.RIGHT)
        increaseSpeed();
    return true;
}

@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
    increaseSpeed();
    return true;
}

This is using the event-based InputProcessor to handle input events.

LibGDX does map desktop mouse events onto "touch" events so you can collapse that part of PC/Android input handling, but there is nothing generic that maps keyboard keys to touch events or anything like that.

OTHER TIPS

You should have a look at this: Interfacing with plaform specific code.

But in your case you would probably not use an interface. The basic idea is that in your different platform specific projects, you would implement an InputAdapter, for example DesktopController extends InputAdapter. Then you would supply this InputAdapter to your ApplicationListener. Somehow like this:

public class MyGame implements ApplicationListener {
    private final InputAdapter controller;

    public MyGame(InputAdapter controller) {
        this.controller = controller;
        // this controller can now be used
        // Gdx.input.setInputProcessor(controller);
    }

}

public static void main(String[] argv) {
    LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    new LwjglApplication(new MyGame(new DesktopController()), config);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top