我使用libgdx 1.9.0进行游戏开发。 我在主屏幕中有一个包含多个按钮的舞台。 我有另一个显示游戏对象的屏幕代码。 当按下Android设备中的后退按钮时,主屏幕中的舞台被禁用 按钮的监听器功能不起作用。 在主屏幕中我有:
public class MainMenuScreen extends ScreenAdapter {
TextButton button_start;
TextButtonStyle textButtonStyle;
BitmapFont font;
FitViewport viewp;
Stage stage;
public MainMenuScreen(IndependentArch game) {
this.game = game;
/* add GUI controls */
stage = new Stage(new StretchViewport(game.SCREEN_WIDTH,
game.SCREEN_Height), game.batcher);
Gdx.input.setInputProcessor(stage);
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = Assets.font_btitr;
textButtonStyle.up = Assets.skin.getDrawable("button");
textButtonStyle.down = Assets.skin.getDrawable("button-down");
button_start = new TextButton(Reshape.doReshape_mirror("start"),
textButtonStyle);
button_start.setBounds(0, 0, 200, 50);
button_start.setPosition(140, 255);
button_about.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
MyTextInputListener listener = new MyTextInputListener();
Gdx.input.getTextInput(listener, "name", "",
"please enter your name.");
}
});
stage.addActor(button_start);
}
public void update() {
if (Gdx.input.justTouched()) {
stage.getCamera().unproject(
touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
}
}
public void draw(float delta) {
Gdx.gl.glClearColor(0.09f, 0.28f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batcher.disableBlending();
game.batcher.begin();
game.batcher.draw(Assets.backgroundRegion[0], 0, 0, game.SCREEN_WIDTH,
game.SCREEN_Height);
game.batcher.end();
stage.act(delta);
stage.draw();
game.batcher.enableBlending();
game.batcher.begin();
game.batcher.draw(Assets.uiskin.findRegion("music"),
game.SCREEN_WIDTH - 90, 10, 80, 80);
game.batcher.end();
}
@Override
public void render(float delta) {
update();
draw(delta);
}}
在游戏画面中我有:
public class GameScreen implements Screen, InputProcessor {
OrthographicCamera guiCam;
Vector3 touchPoint;
World world;
WorldRenderer renderer;
CameraController camController;
InputMultiplexer multiplexer;
GestureDetector gestureDetector;
/** for debug rendering **/
ShapeRenderer debugRenderer = new ShapeRenderer();
int lastScore;
String scoreString;
float totalGameTime;
GlyphLayout glyphLayout = new GlyphLayout();
public GameScreen(IndependentArch game) {
this.game = game;
Gdx.input.setCatchBackKey(true);
state = GAME_READY;
guiCam = new OrthographicCamera(game.SCREEN_WIDTH, game.SCREEN_Height);
guiCam.position.set(game.SCREEN_WIDTH / 2, game.SCREEN_Height / 2, 0);
touchPoint = new Vector3();
world = new World();
renderer = new WorldRenderer(game.batcher, world);
Gdx.input.setInputProcessor(this);
camController = new CameraController(world);
multiplexer = new InputMultiplexer();
multiplexer.addProcessor(new GestureDetector(20, 0.5f, 2, 0.15f,
camController));
multiplexer.addProcessor(this);
Gdx.input.setInputProcessor(multiplexer);
}
// * InputProcessor methods ***************************//
@Override
public boolean keyDown(int keycode) {
if (keycode == Keys.BACK) {
game.setScreen(new MainMenuScreen(game));
// Gdx.app.exit();
}
return false;
}}
答案 0 :(得分:1)
覆盖主菜单屏幕中的stage
方法,并将输入处理器设置为@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
rootSolve