Libgdx,为主菜单添加背景图片

Libgdx, add background image to the main menu

我正在尝试将背景图片添加到游戏的主菜单中。我正在使用 table 作为按钮。我尝试在 render 方法中使用 spritebatch,它可以工作,但是我看不到我的按钮。我知道table有个setBackground方法,试过用了,也没成功。请看我的源码,谢谢!

public class MainMenuScreen extends GameScreen {

    private Stage _stage;
    private MyGame _game;


    public MainMenuScreen(MyGame game) {
        _game = game;

        //creation
        _stage = new Stage();
        Table table = new Table();
        table.setFillParent(true);


        final TextButton newGameButton = new TextButton("New Game", Utility.STATUSUI_SKIN);
        TextButton loadGameButton = new TextButton("Load Game", Utility.STATUSUI_SKIN);
        TextButton watchIntroButton = new TextButton("Watch Intro", Utility.STATUSUI_SKIN);
        TextButton creditsButton = new TextButton("Credits", Utility.STATUSUI_SKIN);
        TextButton exitButton = new TextButton("Exit", Utility.STATUSUI_SKIN);

        //Layout
        table.add(newGameButton).spaceBottom(40).row();
        table.add(loadGameButton).spaceBottom(40).row();
        table.add(watchIntroButton).spaceBottom(40).row();
        table.add(creditsButton).spaceBottom(40).row();
        table.add(exitButton).spaceBottom(40).row();

        _stage.addActor(table);

        //Listeners
        newGameButton.addListener(new ClickListener() {
                                      @Override
                                      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                                          return true;
                                      }

                                      @Override
                                      public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                                          _game.setScreen(_game.getScreenType(ScreenType.NewGame));
                                          clickSound();
                                      }
                                  }
        );

        loadGameButton.addListener(new ClickListener() {

                                       @Override
                                       public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                                           return true;
                                       }

                                       @Override
                                       public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                                           _game.setScreen(_game.getScreenType(ScreenType.LoadGame));
                                           clickSound();
                                       }
                                   }
        );

        exitButton.addListener(new ClickListener() {

                                   @Override
                                   public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                                       return true;
                                   }

                                   @Override
                                   public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                                       clickSound();
                                       Gdx.app.exit();
                                   }

                               }
        );

        watchIntroButton.addListener(new ClickListener() {

                                         @Override
                                         public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                                             return true;
                                         }

                                         @Override
                                         public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                                             MainMenuScreen.this.notify(AudioObserver.AudioCommand.MUSIC_STOP, AudioObserver.AudioTypeEvent.MUSIC_TITLE);
                                             _game.setScreen(_game.getScreenType(ScreenType.WatchIntro));
                                             clickSound();
                                         }
                                     }
        );

        creditsButton.addListener(new ClickListener() {

                                      @Override
                                      public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                                          return true;
                                      }

                                      @Override
                                      public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                                          _game.setScreen(_game.getScreenType(ScreenType.Credits));
                                          clickSound();
                                      }
                                  }
        );

        notify(AudioObserver.AudioCommand.MUSIC_LOAD, AudioObserver.AudioTypeEvent.MUSIC_TITLE);
    }

    public static void clickSound() {
        Sound sound = Gdx.audio.newSound(Gdx.files.internal("audio/NFF-switch-on.wav"));
        sound.play(1F);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        _stage.act(delta);
        _stage.draw();
    }


    @Override
    public void resize(int width, int height) {
        _stage.getViewport().setScreenSize(width, height);
    }

    @Override
    public void show() {
        notify(AudioObserver.AudioCommand.MUSIC_PLAY_LOOP, AudioObserver.AudioTypeEvent.MUSIC_TITLE);
        Gdx.input.setInputProcessor(_stage);
    }

    @Override
    public void hide() {
        Gdx.input.setInputProcessor(null);
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        _stage.dispose();
    }

}

您没有在代码中将背景设置为 table。这是我的一个项目的示例,该项目从九个图像中设置 table 背景。

table.setBackground(new NinePatchDrawable(atlas.createPatch("background")));

此代码是使用默认 badlogic.jpg 图像作为 table.

背景的完整工作示例
package com.mygdx.gtest;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class Test extends ApplicationAdapter{

    private Stage stage;

    @Override
    public void create() {
        stage = new Stage();
        Table testTable = new Table();
        testTable.setBackground(new TextureRegionDrawable(new TextureRegion(new Texture("badlogic.jpg"))));
        testTable.setFillParent(true);
        testTable.setDebug(true);
        stage.addActor(testTable);
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        stage.act();
        stage.draw();

    }
}