【发布时间】:2022-11-07 09:50:14
【问题描述】:
我目前正在研究一个小型 libGdx 项目,我现在面临一些关于透明屏幕的麻烦。这个透明屏幕被称为“GameMenuScreen”,因为它可以在整个游戏过程中被调用。如果它被称为“GameMenuScreen”,则在 GameScreen 上方呈现,该 GameScreen 在背景中仍然可见。现在当我按下例如“保存”按钮,所有按钮都重复。我认为它的发生是因为屏幕不是每帧都刷新。但现在我不知道如何刷新屏幕并保持其透明度。 这是“GameMenuScreen”的代码:
public class GameMenuScreen extends ScreenAdapter {
private TextureAtlas uiskinAtlas;
private Skin skin;
private OrthographicCamera camera;
private Viewport viewport;
private Stage stage;
private Table table;
private Vector3 position;
private SpriteBatch batch;
private TextButton continueGame;
private TextButton loadGame;
private TextButton settings;
private TextButton mainMenu;
private TextButton quit;
private TextButton save;
public GameMenuScreen() {
uiskinAtlas = GameWindow.manager.get(Paths.uiskinAtlas, TextureAtlas.class);
skin = new Skin(Gdx.files.internal(Paths.uiskinJson), uiskinAtlas);
camera = new OrthographicCamera();
position = new Vector3(Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2, 0);
camera.position.set(position);
viewport = new ScreenViewport(camera);
stage = new Stage(viewport);
batch = new SpriteBatch();
table = new Table();
continueGame = new TextButton("Continue Game", skin);
loadGame = new TextButton("Load Game", skin);
settings = new TextButton("Settings", skin);
mainMenu = new TextButton("Main Menu", skin);
quit = new TextButton("Quit", skin);
save = new TextButton("Save", skin);
continueGame.setColor(Color.ROYAL);
loadGame.setColor(Color.ROYAL);
settings.setColor(Color.ROYAL);
mainMenu.setColor(Color.ROYAL);
quit.setColor(Color.ROYAL);
save.setColor(Color.ROYAL);
createUI();
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 0f);
batch.enableBlending();
batch.begin();
continueGame.draw(batch, 0);
loadGame.draw(batch, 0);
settings.draw(batch, 0);
mainMenu.draw(batch, 0);
quit.draw(batch, 0);
save.draw(batch, 0);
batch.end();
stage.act();
stage.draw();
}
private void createUI() {
table.setFillParent(true);
table.center();
setButtonInput();
table.add(continueGame).width(250);
table.row().space(5, 5, 0, 0);
table.add(loadGame).width(250);
table.row().space(5, 5, 0, 0);
table.add(save).width(250);
table.row().space(5, 5, 0, 0);
table.add(settings).width(250);
table.row().space(5, 5, 0, 0);
table.add(mainMenu).width(250);
table.row().space(5, 5, 0, 0);
table.add(quit).width(250);
table.row().space(5, 5, 0, 0);
stage.addActor(table);
}
private void setButtonInput() {
continueGame.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
GameWindow.INSTANCE.setScreen(Screens.getGameScreen());
}
});
loadGame.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Savegame.load();
Screens.getGameScreen().setWorldCreated(false);
GameWindow.INSTANCE.setScreen(Screens.getGameScreen());
}
});
save.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Savegame.save();
}
});
mainMenu.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
GameWindow.INSTANCE.setScreen(Screens.getMainMenuScreen());
}
});
quit.addListener(new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
position = new Vector3(viewport.getScreenWidth() / 2, viewport.getScreenHeight() / 2, 0);
camera.position.set(position);
}
每帧刷新屏幕的正常方法是调用这两个代码段:
Gdx.gl.glClearColor(0.35f, 0, 0f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
但如前所述:屏幕在调用时会失去透明度。
我希望你能帮助我。
【问题讨论】:
-
欢迎来到 SO!请写清楚的问题陈述。你可能想看看"Why is "Can someone help me?" not an actual question?"
-
你会为这篇文章推荐什么明确的问题陈述?
-
有很多方法可以在这里表达一个特定的问题,我没有信心在你的情况下向你推荐一个。也许类似于“为什么我的代码的行为方式与其预期的方式不同,如何解决?“(这将反映你想要什么,但可能会给你一个”你试过调试吗?” 回应,我认为这是一个公平的回应)。请注意,definition effort 在提问时是您自己的责任的一部分。
标签: java libgdx screen transparency game-development