问题
I'm new to java and libgdx and I want to create a main menu screen, Can someone give me a simple example?
回答1:
What you are asking is very broad, it involves many elements like creating buttons, skins, setting up Tables, etc. Anyway you should use Screens for this, add a stage and add actors to the stage. Eventually you need to add Listeners to your button actors to switch screens. Here is one I made for you:
public class TestScreen implements Screen{
private SpriteBatch batch;
protected Stage stage;
private Viewport viewport;
private OrthographicCamera camera;
private TextureAtlas atlas;
protected Skin skin;
public TestScreen()
{
atlas = new TextureAtlas("skin.atlas");
skin = new Skin(Gdx.files.internal("skin.json"), atlas);
batch = new SpriteBatch();
camera = new OrthographicCamera();
viewport = new FitViewport(Constants.WorldWidth, Constants.WorldHeight, camera);
viewport.apply();
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
camera.update();
stage = new Stage(viewport, batch);
}
@Override
public void show() {
//Stage should controll input:
Gdx.input.setInputProcessor(stage);
//Create Table
Table mainTable = new Table();
//Set table to fill stage
mainTable.setFillParent(true);
//Set alignment of contents in the table.
mainTable.top();
//Create buttons
TextButton playButton = new TextButton("Play", skin);
TextButton optionsButton = new TextButton("Options", skin);
TextButton exitButton = new TextButton("Exit", skin);
//Add listeners to buttons
playButton.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
((Game)Gdx.app.getApplicationListener()).setScreen(new PlayScreen());
}
});
exitButton.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
//Add buttons to table
mainTable.add(playButton);
mainTable.row();
mainTable.add(optionsButton);
mainTable.row();
mainTable.add(exitButton);
//Add table to stage
stage.addActor(mainTable);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(.1f, .12f, .16f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0);
camera.update();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
skin.dispose();
atlas.dispose();
}
}
The way I call this is by changing the initial class a bit.
//Let the class extend from game
public class MyGame extends Game()
{
//Delete everything in it and leave a create() with a single line
@Override
public void create() {
setScreen(new MenuScreen());
}
}
Of course to make the above code work you need to setup a Skin
and Atlas
for drawing the buttons. You could however just add a image and a font and create your buttons manually. Anyway, I just answered a question where I go in depth on creating a Skin
and Atlas
.
Edit Although an example of a menu class has been asked the user asker actually just needed to know how to switch from screen to screen. A bit awkward but luckely writing the above code just took a couple of minutes ;).
You can always access the ApplicationListener
from anywhere using Gdx.app.getApplicationListener
. You can cast this to Game
to access setScreen
.
((Game)Gdx.app.getApplicationListener()).setScreen(new GameScreen());
Or you could pass along the initial Game object or applicationListener by hand. Make sure the new screen accepts the game object.
public class MenuScreen
{
private Game gameObject;
public MenuScreen(Game gameObject)
{
this.gameObject = gameObject;
}
private void someMethod()
{
//Switches to a new MenuScreen...
//useless in most cases but you get the idea
gameObject.setScreen(new MenuScreen(gameObject);
}
}
来源:https://stackoverflow.com/questions/32451921/how-to-create-libgdx-main-menu-screen