java 如何创建 libgdx 主菜单屏幕
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32451921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create libgdx main menu screen
提问by Kevin Bryan
I'm new to java and libgdx and I want to create a main menu screen, Can someone give me a simple example?
我是java和libgdx的新手,我想创建一个主菜单屏幕,有人能给我一个简单的例子吗?
回答by Madmenyo
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:
您要问的范围很广,它涉及许多元素,例如创建按钮、皮肤、设置表格等。无论如何,您应该为此使用屏幕,添加舞台并向舞台添加演员。最终,您需要将 Listeners 添加到您的按钮 actor 以切换屏幕。这是我为你制作的:
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 questionwhere I go in depth on creating a Skin
and Atlas
.
当然,要使上面的代码工作,您需要设置一个Skin
和Atlas
来绘制按钮。但是,您可以只添加图像和字体并手动创建按钮。无论如何,我刚刚回答了一个问题,其中我深入研究了创建Skin
和Atlas
.
EditAlthough 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
.
您始终可以ApplicationListener
从任何地方使用Gdx.app.getApplicationListener
. 您可以将其转换Game
为访问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.
或者,您可以手动传递初始 Game 对象或 applicationListener。确保新屏幕接受游戏对象。
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);
}
}