【发布时间】:2018-08-14 05:45:36
【问题描述】:
我正在尝试将背景图片添加到我的游戏主菜单中。我正在使用表格作为按钮。我尝试在渲染方法中使用 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();
}
}
【问题讨论】:
标签: java android user-interface background libgdx