User Input

Now we know how to create a basic libGDX “game” that consists of some simple animated shapes bouncing around the scene. This demonstrates the lifecycle and animation framework that libGDX provides, but it’s not very interesting because it doesn’t respond to user input yet.


This is a companion discussion topic for the original entry at https://happycoding.io/tutorials/libgdx/input

Hi, I’m fairly new to Java so this might be a stupid question, but I was wondering why the touchDown function has the “(int x, int y, int pointer, int button)” parameters if the method itself just randomizes the colors and doesn’t use those variables?

1 Like

That’s a really good question!

It’s because in Java, when you override a function, you need to match the function signature exactly. In other words, you need to take all of the parameters that the function in the parent class or interface takes, even if you don’t use them in your function.

To see what I mean, try getting rid of the parameters, and you’ll see a compiler error!

Hi there! Thank you for the tutorial and sorry for my bad english.
I’ve created a public class MyInputProcessor which implements InputProcessor(separate file MyInputProcessor.java)
Also I have a GameScreen class which implements Screen.

I want to write in MyInputProcessor something like:
“If touched AND GameScreen.object.overlaps current touch position then…”
The problem is I cant get access to GameScreen class. Do I have to use static variable in GameClass? Because Afaik i cant create GameScreen object. Is this a bad practice? Can i reach desired result (input logic in separate class) without static variables? Or maybe should I use inputAdapter and override touch event in my GameScreen file?

@Tsumimi_Tyan hi there! Generally speaking, you’d create an instance of one class and then pass that instance in to your other class, probably through the constructor or a setter function. So your MyInputProcessor constructor might look like this:

public MyInputProcessor(GameScreen gameScreen) {
  this.gameScreen = gameScreen;
}

Then in other functions in your MyInputProcessor, you could reference the gameScreen variable to access its object member.