问题描述
我是java和libgdx的新手,我想创建一个主菜单屏幕,有人可以给我一个简单的例子吗?
I'm new to java and libgdx and I want to create a main menu screen, Can someone give me a simple example?
推荐答案
你所要求的是非常广泛的,它涉及许多元素,如创建按钮,皮肤,设置表等等。无论如何你应该使用Screens,添加一个舞台并在舞台上添加actor。最终,您需要将Listeners添加到按钮actor以切换屏幕。这是我为你做的一个:
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());
}
}
当然要使上面的代码工作,你需要设置 Skin
和 Atlas
以绘制按钮。但是,您可以只添加图像和字体并手动创建按钮。无论如何,我只是深入创建皮肤
和 Atlas
。
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 ;).
你可以随时访问 ApplicationListener
从任何地方使用 Gdx.app.getApplicationListener
。您可以将其转换为游戏
以访问 setScreen
。
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());
或者您可以手动传递初始Game对象或applicationListener。确保新屏幕接受游戏对象。
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);
}
}
这篇关于如何创建libgdx主菜单屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!