12

I'm working on game made with libgdx that needs some GUI above my game screen. Something like FrameLayout in Android.

I have GameScreen where everything is happening. What I want now is to add a "pause" button, highscore information etc.

I've tried to combine a Stage object with regular sprite drawing. But I had some problems with handling inputs: how to manage if user clicked pause button in stage, or clicked game area (where I should add some bullets)...

Ishan
  • 3,303
  • 5
  • 29
  • 47
Veljko
  • 1,893
  • 6
  • 28
  • 58

1 Answers1

23

You should be able to use a Stage to manage your UI. To get input working correctly, you'll need to add an InputMultiplexer so that the Stage and then your current input scheme will both get the inputs.

To set it up, you'll do something like this:

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

(Code sample based on code from https://code.google.com/p/libgdx/wiki/InputEvent)

Note that the order is important (I'm guessing you'll want the stage to get events first to see if the UI is being touched or not). Also, the boolean return value from input event handlers are more important with a multiplexer, as "handled" events will not be propagated by the mutliplexer. UI events inside the Stage have their own "handled" flag (mostly it does the right thing but there are some subtle differences).

One alternative to the InputMultiplexer would be to create a "GameScreenActor" (a new subclass of Actor) that contains your current game screen that you plug into the global Stage. You'd have to move your input processing to the scene2d approach, though. This probably isn't the right choice for you, but it is a viable one.

P.T.
  • 24,557
  • 7
  • 64
  • 95
  • Ok, I though so...I've checked InputMultiplexer documentation...your answer makes sence. I will try this aproach....hopefully it will works. Tnx a lot! :) I'm familiar with handling inputs in Android, I hope it works similiar :) – Veljko Mar 04 '13 at 15:44