编辑:第一个复制者“第二个”是正确的。我忘了设置Gdx.input.setInputProcessor(stage); -现在一切正常。谢谢!
我的Libgdx按钮“返回”,“菜单”和“ TryAgain”(将您带回到另一个屏幕)不起作用。
我尝试了所有可以找到的解决方案,但是它们仍然无法正常工作。我还在其他“屏幕”中使用“按钮”和“舞台”,它们的工作方式都非常吸引人。
你们在代码中看到任何错误吗? CreateStage()
创建舞台,并通过render方法显示它们。
非常感谢您的帮助!
public class HighscoreScreen extends ScreenAdapter {
private final GameClass game;
private int score;
private String name;
private boolean menuView;
private SpriteBatch batch;
private BitmapFont scoreFont;
private String[] players;
private int[] scores;
private int counter = 0;
private OrthographicCamera camera;
private Stage stage;
private ScreenViewport v;
public HighscoreScreen (GameClass game, OrthographicCamera camera, int score, String name) {
this.camera = camera;
this.game = game;
this.score = score;
this.name = name;
this.batch = new SpriteBatch();
menuView = false;
batch = new SpriteBatch();
scoreFont = new BitmapFont(Gdx.files.internal("Fonts/score.fnt"),
Gdx.files.internal("Fonts/score.png"),false);
createStage();
}
public HighscoreScreen (GameClass game, OrthographicCamera camera){
this.game = game;
this.camera = camera;
this.batch = new SpriteBatch();
menuView = true;
scoreFont = new BitmapFont(Gdx.files.internal("Fonts/score.fnt"),
Gdx.files.internal("Fonts/score.png"),false);
}
public void createStage(){
v = new ScreenViewport();
v.setCamera(camera);
stage = new Stage(v, batch);
if (menuView) {
Buttons back = new Buttons("Back");
stage.addActor(back.createButton(GameInfo.WIDTH / 2 - 100,
GameInfo.HEIGHT / 2 - 200));
back.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new MenuScreen(game));
}
});
} else {
Buttons tryAgain = new Buttons("Try Again");
stage.addActor(tryAgain.createButton(GameInfo.WIDTH / 2 - 200,
GameInfo.HEIGHT / 2 - 200));
tryAgain.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new SurvivalScreen(game, name));
}
});
Buttons menu = new Buttons("Menu");
stage.addActor(menu.createButton(GameInfo.WIDTH / 2,
GameInfo.HEIGHT / 2 - 200));
menu.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
game.setScreen(new MenuScreen(game));
}
});
}
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (menuView) {
if (counter == 0) {
createStage();
writeScoresToFile();
readHighscore();
counter++;
}
batch.begin();
highscoreToFont();
batch.end();
stage.act();
stage.draw();
} else {
if (counter == 0) {
createStage();
writeScoresToFile();
readHighscore();
compareHighscore();
writeHighscore();
counter++;
}
batch.begin();
highscoreToFont();
batch.end();
stage.act();
stage.draw();
}
}
public void dispose(){
batch.dispose();
stage.dispose();
}
答案 0 :(得分:0)
您可能在舞台上缺少输入处理器的设置(至少不在您发布的代码中)。
Gdx.input.setInputProcessor(stage);
注意:
假设Buttons类返回一个适当的actor。
(请参阅上面的评论)