There are three main ways (that I know of) to obtaining input in LibGDX.
The first is as you said, changing the ClickListener
, the second will be setting the setting the current screen as an implementation of InputProcessor
, and the third will be obtaining the mouse click through a new class, or a sub-class to get the input.
I'll elaborate on each:
- The first way: Changing the ClickListener. Pretty straight forward, lets say you have a text button named
onOffSwitch
, if you'd want to recieve input on it, you'd write the following code:
onOffSwitch.addListener(new ClickListener(){ });
This will create a new ClickListener
to use to recieve any input from the button.
When you use this you can add method like enter
to see whenever the mouse is hovering over the buttton, or when the player is touching the button.
- The second way: Setting a new
InputProcessor
, this will basically create an inputprocessor which will be your entire screen.
I don't recommend using this since it requires more than one methhod and is often not needed, all that's needed is a little digging around on how to get the listeners you need.
To implement this way, you need to make your class implement the InputProcessor
and implement the needed methods.
The third and last way, which I assume is the one you'll use is the following:
To use this way you'll create a new instance of InputProcessor
and apply it to Gdx.input
:
class AwesomeGameClass {
public void init() {
Gdx.input.setInputProcessor(new InputProcessor() {
@Override
public boolean TouchDown(int x, int y, int pointer, int button) {
if (button == Input.Buttons.LEFT) {
//check if the x and y coordinates are the same as your text button (using TextButton.getPrefWidth() & TextButton.getPrefHeight()) and if so then invoke the onMouseDown method.
return true;
}
return false
}
});
}
private void onMouseDown() {
}
}
The code I got from here, and you can see what else there is there that I might have missed. HERE
Except that you can read up on materials in the LibGDX github, which is located HERE
Or look at the data concerning TextButtons in HERE