问题
Basically, I have a screen where I have a character and a button. Character jumps when I touch the screen (implemented using Gdx.input.justTouched). The button opens PauseMenu when clicked (button is an actor of a stage and click is implemented using button.addListener(new ChangeListener() {.... }). My InputProcessor is set on stage
Gdx.input.setInputProcessor(stage);
The problem is when the button is clicked my character jumps (but he shouldn't) and the pause menu opens. I was searching through InputMultiplexer but that didn't work (or maybe I used it in a wrong way).
Thanks for any advice!
回答1:
You can use the InputMultiplexer
.
First how the InputMultiplexer
works.
In the InputMultiplexer
you can do many InputProcessor
s and an InputProcessor
has methods which return boolean.
This boolean value says whether the event was handled or not.
So if the method returns false, the InputMultiplexer
give the event to the next InputProcessor
.
If the method returns true the event was handled and the event won't go to the next InputProcessor
.
Now we make an InputProcessor
for our screen in this case TestScreenInput
:
public class TestScreenInput implements InputProcessor {
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
System.out.println("Touch down");
character.jump();
return true;
}
@Override
... //all other methods from InputProcessor
}
The touchDown
method returns true, so the next InputProcessor won't become the touchDown
event
In our screen class (TestScreen) we create our Stage
:
public class TestScreen implements Screen {
private Stage stage;
@Override
public void show() {
stage = new Stage();
}
}
Now we will create our TextButton
with a ChangeListener
.
The problem is, how we can say that the event is handled?
The ChangeListener::changed(ChangeEvent event, Actor actor)
doesn't return a boolean.
When we look into the Stage
class we can find the touchDown
method and the method returns:
boolean handled = event.isHandled();
return handled;
event
is a Type of Event
and ChangeEvent
extends Event
In our changed(ChangeEvent event, Actor actor)
method we have a ChangeEvent
. So all we have to do is to set this event handled.
In Event
there is a method:
public void handle () { handled = true;}
Now we know all to create our Button:
TextButton button = new TextButton("Click", skin);
button.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
System.out.println("Click Button");
event.handle(); //set the event handled = true
}
});
stage.addActor(button);
Finally, we create our InputMultiplexer
. Important is that the stage come before our TestScreenInput
because the TestScreenInput
will mark the touchDown
as handled and the stage
never will receive them.
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(stage);
multiplexer.addProcessor(new TestScreenInput());
Gdx.input.setInputProcessor(multiplexer);
来源:https://stackoverflow.com/questions/52412742/screen-responds-when-button-is-clicked