Search code examples
javaandroiduser-interfacebackgroundlibgdx

Libgdx, add background image to the main menu


I am trying to add background image to my main menu of my game. I am using table for buttons. I tried to use spritebatch in render method, and it works, but then I cannot see my buttons. I know table has a setBackground method, tried to use it, also not successful. Please look at my source code, Thank you!

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

}

Solution

  • You have not set the background to the table in your code. This is an example from one of my projects that sets the table background from a ninepatch image.

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

    This code is a complete working example of using the default badlogic.jpg image as a background for a 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();
    
        }
    }