我正在用libgdx和java做我的第一个游戏。
我创建了两个游戏屏幕,菜单屏幕有播放按钮和退出按钮。我想玩游戏时,我点击播放按钮,一切似乎都很好,在menuscreen。但在gamescreen中,当我点击play按钮的同一位置时,游戏又开始了。同样的事情也发生在退出按钮上:如果我在放置退出按钮的地方单击,游戏将退出。
有人能帮我解决这个问题吗?如果我的英语和描述不好,谢谢你,对不起。
菜单屏幕类:
public class GameMenuScreen extends InputAdapter implements Screen {
BommanGame game;
Stage menuStage;
Texture backgroundTexture;
TextButton playButton;
TextButton exitButton;
public GameMenuScreen(final BommanGame game) {
this.game = game;
menuStage = new Stage(new ScreenViewport());
backgroundTexture = new Texture(Gdx.files.internal("ui/img/background.png"));
playButton = new TextButton("Play", game.buttonStyle);
playButton.setPosition(380, 200);
playButton.addListener(new InputListener() {
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
game.setScreen(new GamePlayScreen(game));
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
exitButton = new TextButton("Exit", game.buttonStyle);
exitButton.setPosition(380, 126);
exitButton.addListener(new InputListener() {
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.exit();
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}
});
menuStage.addActor(playButton);
menuStage.addActor(exitButton);
Gdx.input.setInputProcessor(menuStage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.spriteBatch.begin();
game.spriteBatch.draw(backgroundTexture, 0, 0);
game.spriteBatch.end();
menuStage.act();
menuStage.draw();
}
@Override
public void dispose() {
game.spriteBatch.dispose();
backgroundTexture.dispose();
menuStage.dispose();
}
游戏屏幕类:
public class GamePlayScreen implements Screen {
BommanGame game;
private float elapsedTime = 0;
/*game components*/
public GamePlayScreen(BommanGame game) {
this.game = game;
/*game component constructors*/
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
elapsedTime += Gdx.graphics.getDeltaTime();
game.spriteBatch.begin();
/*render game components*/
game.spriteBatch.end();
}
@Override
public void dispose() {
game.spriteBatch.dispose();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!