简体   繁体   中英

libGDX Mouse Input and scene2d — Not Receiving Proper Input?

I am having trouble with buttons.

stage = new Stage();

Skin skin = new Skin(Gdx.files.internal("assets/uiskin.json"));

Table table = new Table();
table.setDebug(true);
table.setFillParent(true);
stage.addActor(table);

Button button = new Button(skin, "default");

table.add(button);

The code above creates a button with the proper text in center of the screen, but input is broken. When I click on the button, it does not register input. However when I click ABOVE the button, it registers input. What am I doing wrong?

Regarding the code that you posted, you will need to set the input processor to the stage. By doing this, libGDX will use this as a Listener to register events that happen. Try adding this to your block of code:

    Gdx.input.setInputProcessor(stage);

Also, if you are not planning on adding other Actors to your Stage, I would just add the Button directly to the Stage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM