2

I don't understand how InputProcessor is supposed to work.

I have multiple Screens for a game. I created a MyInputProcessor class that implements InputProcessor. In my MenuState class I Gdx.input.setInputProcessor to an instance of the class.

  1. First of all, how should I access and set variables that are defined in my MainMenu class in MyInputProcessor? If I want the touchDown method to change a variable for example.

  2. If I switch Screens, do I have to create a new InputProcessor class to check for other touch events? I obviously don't want it to continue checking for things meant for MainMenu class. How am I supposed to use it?

  3. Am I just supposed to create a whole new InputProcessor for each Screen?

I find this all very confusing. Thankful for any help at all.

S5amuel
  • 117
  • 11
  • 1
    Input is usually tightly coupled to each screen so a common pattern is to have each screen implement InputProcessor, then call Gdx.input.setInputProcessor(this) in the screen's show() method. Basically, yes you'll need to define what to do with input on each screen. Using Stages is another possibility that you can choose to ignore for now. Stage also implements InputProcessor. Commonly, people use Stages for UI things (buttons, input). – Barodapride Oct 12 '16 at 21:32
  • Thank you very much for clarifying. InputProcessor has confused the hell out of me. – S5amuel Oct 13 '16 at 06:58

2 Answers2

6

Yes, usually you create one InputProcessor for every Screen, or even better for every object, that needs to process inputs. This object can be a Screen, but it can be a Player to.
So every object that needs to get notified about any input should implement InputProcessor and handle the relevant inputs.
Also make sure to set your InputProcessor as the current, active one (using Gdx.input.setInputProcessor).
The Screens for example could set themself as the current InputProcessor in the show method (and eventually unregister themself in the hide). IF you want to use multiple InputProcessors at once (for example in the GameScreen, where the Player is controlled using "w,a,s,d", but you want to show a PauseMenu on "Esc"), just use InputMultiplexer and register every InputProcessor on that multiplexer.
If you use InputMultiplexer, make sure to take care about the return value of your InputProcessor-methods:
- Return true, if the proccessor handled the event (for example in the Players InputProcessor, when "w", "a", "s" or "d" is pressed). - Return false, when you did not handle the event (for example in the Players InputProcessor, when "Esc" is pressed).
The InputMulitplexer will go through all it's InputProcessors and send them the event, unitl one of them returns true. All others won't get notified about the event.
Also note, that Stage is an InputProcessor, which distributes the event to it's Actors. So if you want to handle inputs in your Actors, make sure to set Stage as you current InputProcessor.

Robert P
  • 9,398
  • 10
  • 58
  • 100
  • Greatly appreciate your help. Would you suggest using stages when making basic mobile games with one touch handlat – S5amuel Oct 13 '16 at 07:02
  • Accidentally sent the above comment. Continuing from "one": touch game play? Or is it overkill? I don't have any experience with it, should I have? – S5amuel Oct 13 '16 at 07:04
  • Well, `Stage` has some advantages but also some drawbacks. It is perfekt for any UI (like menus) and also smaller games. Stage already supports clicks/touches on `Actor`s, while without `Stage` you would have to search for the touched object yourself. On the other hand `Stage` makes it [almost impossible using an MVC-pattern](http://stackoverflow.com/questions/21238638/using-mvc-with-libgdx-scene2d) which is pretty important for bigger games. So I would definitely use `Stage` for UI and consider using it for smaller games. But take a look at it's documentation on the Libgdx wiki! – Robert P Oct 13 '16 at 07:08
  • I definitely will. Thank you! – S5amuel Oct 13 '16 at 07:11
  • If you want to use `Stage`, you should read the [documentation](https://github.com/libgdx/libgdx/wiki/Scene2d). Other experience is not needed, you always start new things without experience :) But ofc you should start small, maybe try to create menus before starting with the actualy gameplay? – Robert P Oct 13 '16 at 07:11
1

Are you using Stage as your InputProcessor? If yes I, assume you have separate Stage instance for every screen.

You should add Actors to the stage and let them handle the input. If you want to combine more input processors you do this:

InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(yourCustomInputProcessor);
multiplexer.addProcessor(stage)
Gdx.input.setInputProcessor(multiplexer);

EDIT Don't call Gdx.input.setInputProcessor from constructor of the Main screen but rather in the moment when it appears.

Heisenbug
  • 951
  • 6
  • 25
  • Thanks for the answer. I will have to read up on this though as I am completely lost. Right now I just have a class implementing `InputProcessor` which I create an instance off when I change Screen checking if stuff is happening. I don't use stages as I've never encountered them before. – S5amuel Oct 12 '16 at 20:20
  • Stages are pretty good, you just create whatever Actor you like and call `stage.update()` and `stage.draw()` in draw method of your screen. `stage.update()` will call method `act()` on every actor it contains. So when you are creating extending `Actor` you need to override those 2 methods. – Heisenbug Oct 12 '16 at 20:43
  • Thanks for your help. I will learn more about stages and actors. – S5amuel Oct 13 '16 at 07:09