Libgdx,将背景图像添加到主菜单

时间:2018-03-06 11:20:36

标签: java android user-interface background libgdx

我正在尝试将背景图片添加到我的游戏主菜单中。我正在使用桌子按钮。我尝试在渲染方法中使用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();
    }

}

1 个答案:

答案 0 :(得分:0)

您尚未在代码中为表格设置背景。这是我的一个项目中的示例,该项目从九个图像设置表格背景。

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

此代码是使用默认badlogic.jpg图像作为表格背景的完整工作示例。

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();

    }
}