Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Ok, so my question is simple. How do you poll input with a GUI class without it affecting what's underneath the GUI? So suppose you have an RPG game with a gui for the d-pad and other controls:

gui.getInput(); //User taps button in the gui
world.getInput(); //The tap should have only affected the gui, but instead goes through and also affects the world

How can you deal with this problem? I thought of some ways but I don't like them.

share|improve this question
    
Why does world have a getInput() method? What is it supposed to do? –  congusbongus Apr 2 at 4:39
    
@congusbongus well thats not the point but its for the player or something. Its an example –  user3466304 Apr 2 at 11:37
    
You could just have a Boolean that is set true if gui input fires and then skips world input –  Krtko Apr 2 at 19:29
add comment

1 Answer

up vote 1 down vote accepted

Trust me look into Scene2D. It handles everything input related for you. You can even do timed based actions on it, which are very nice. Its also built into libGDX. Truuuuust me its super easy once you get the hang of it. Honestly, I now write everything in Scene2D.

If your having issues getting started with it, Heres a little sample code for you

 Stage stage;
    ImageButton title; //(there are several types of these, even just simply Image)
    InputListener titleListen; //(also several variants)


 ///----------------------------------------
    //init phase
            stage = new Stage();
            stage.setViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
            Gdx.input.setInputProcessor(stage); //important


            titleListen = new InputListener(){
    //many different types of inputs to override
                @Override
                public boolean touchDown(InputEvent event, float x, float y,
                        int pointer, int button) {
                    //something
                    return super.touchDown(event, x, y, pointer, button);
                }

            };




    ImageButtonStyle style = new ImageButtonStyle();
            style.up = new TextureRegionDrawable(assets.title); //has to be a textureregion
            style.down = new TextureRegionDrawable(assets.title);
            title = new ImageButton(style);
            title.setSize(title.getWidth()*assets.scaleX, title.getHeight()*assets.scaleY);
            title.setPosition(whereever);
            stage.addActor(title);//adds it to the Scene
            play.addListener(titleListen); //adds the listener

  ///----------------------------------------  
    //game loop
//
//
//
    stage.act(delta); //updates Scene and grabs inputs

    stage.draw(); //draws Scene
share|improve this answer
    
Thanks I think I'll use it. I was looking for input multiplexer –  user3466304 Apr 8 at 3:26
    
Scene2D is very nice! –  Krtko Apr 8 at 3:27
1  
I love how you can use virtual joysticks and buttons and scroll bars and everything that a ui should have. It seems very useful! –  user3466304 Apr 8 at 3:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.