如何让Libgdx游戏启动时首先显示主菜单界面?
Hey there! Let’s break down how to add a main menu that loads first in your Mario-style game using the screen system from Brent Aurelis’ tutorials.
First, I’m assuming you’re using a base Screen class (like the one Brent covers) that handles rendering, input, and lifecycle events. Here’s how to adjust that system to prioritize the main menu:
1. Create a MainMenuScreen Class
Build your main menu by extending the same Screen class you used for the game level. This class will handle drawing menu options and processing user input to navigate or select actions:
public class MainMenuScreen extends Screen { private final Game game; public MainMenuScreen(Game game) { this.game = game; } @Override public void render(float delta) { // Clear screen and draw menu elements Gdx.gl.glClearColor(0, 0, 0.1f, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); game.getBatch().begin(); // Draw retro-style menu text (tweak positions/sizes to match your game) game.getFont().draw(game.getBatch(), "MARIO BROTHERS", 120, 320); game.getFont().draw(game.getBatch(), "Press ENTER to Start", 120, 220); game.getFont().draw(game.getBatch(), "Press ESC to Quit", 120, 170); game.getBatch().end(); } @Override public void handleInput() { // Switch to game screen when ENTER is pressed if (Gdx.input.isKeyJustPressed(Input.Keys.ENTER)) { game.setScreen(new GameScreen(game)); dispose(); // Clean up menu resources since we won't need it right away } // Exit game if ESC is pressed if (Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE)) { Gdx.app.exit(); } } // Implement required Screen lifecycle methods @Override public void show() {} @Override public void resize(int width, int height) {} @Override public void pause() {} @Override public void resume() {} @Override public void hide() {} @Override public void dispose() {} }
2. Update Your Main Game Class to Load the Menu First
In your main Game subclass (the entry point of your game), replace the direct initialization of your game screen with the MainMenuScreen:
public class MarioGame extends Game { private SpriteBatch batch; private BitmapFont font; @Override public void create() { batch = new SpriteBatch(); font = new BitmapFont(); // Swap this for a custom retro font if you want // Start with the main menu instead of the game level setScreen(new MainMenuScreen(this)); } // Getters for screens to access shared resources public SpriteBatch getBatch() { return batch; } public BitmapFont getFont() { return font; } @Override public void dispose() { batch.dispose(); font.dispose(); } }
3. Smooth Out Screen Transitions (Quick Tips)
- If you want to let players return to the menu from the game, add a button/key press in
GameScreenthat switches back to a newMainMenuScreeninstance. - Always call
dispose()on screens you’re done with to free up memory (like the menu after starting the game). - Double-check input handling to make sure menu inputs don’t leak into the game screen after switching.
This approach fits perfectly with the screen-based architecture Brent teaches—each screen manages its own state, and your game just swaps between them as needed. The main menu loads first, and the player controls when to jump into the actual game.
内容的提问来源于stack exchange,提问作者Tiago Almeida

