【发布时间】:2018-05-03 09:44:38
【问题描述】:
所以我的问题是,当游戏结束时,它会将游戏扔到一个新屏幕(称为“EndState”)中,我在其中绘制了标题“Game Over”和一个名为“Restart”的 ImageButton,因此背景和玩家以及所有内容else 与前一个屏幕相同,但冻结。当我单击我的重新启动按钮时,它在下方移动 10 像素(按下效果,使用pressedOffsetY 实现),但我在它后面看到相同的按钮,我发现解决这个问题的唯一方法是在绘制舞台之前在整个屏幕上绘制一些东西使用此按钮,但这样我会丢失我的播放器图像以及前一屏幕中的所有其他内容。
我以这种方式使用 Stage 绘制 ImageButton(因为它是可点击的):
public class EndState implements Screen {
private Game endGame;
private Texture gameOver, restartBtnTexture; // textures of title and button
private SpriteBatch batch;
private ImageButton restartImgButton; // ImageButton
private Drawable drawable; //drawable, to store the image and then use it in Style
private Stage stage; //the stage which will contain button
private ImageButton.ImageButtonStyle style; // Style
public EndState(final Game game) {
this.endGame = game; //I receive game state from the previous state
gameOver = new Texture(Gdx.files.internal("GameOver.png"));
restartBtnTexture = new Texture(Gdx.files.internal("Buttons/Button_1.png"));
drawable = new TextureRegionDrawable(new TextureRegion(restartBtnTexture));
batch = new SpriteBatch();
style = new ImageButton.ImageButtonStyle(); // creating style
style.imageUp = drawable; 9
style.pressedOffsetY = -10; // when I press the button I move 10px below
restartImgButton = new ImageButton(style); // creating the button and assigning the Style
restartImgButton.getImage().setFillParent(true);
restartImgButton.setBounds(Gdx.graphics.getWidth() / 2 - (Gdx.graphics.getWidth() / 4), Gdx.graphics.getHeight() * 4/8, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 7); // size and position
restartImgButton.getImageCell().expand().fill();
stage = new Stage(); // creating stage
stage.addActor(restartImgButton); //adding button to the stage
restartImgButton.addListener(new ChangeListener() {
@Override
public void changed(ChangeEvent event, Actor actor) {
endGame.setScreen(new PlayState(endGame));
}
});
Gdx.input.setInputProcessor(stage);
}
@Override
public void show() {
}
@Override
public void render(float delta) {
batch.begin();
batch.draw(gameOver, Gdx.graphics.getWidth() / 2 - (Gdx.graphics.getWidth() * 2/6), Gdx.graphics.getHeight() * 6/8, Gdx.graphics.getWidth() * 2/3, Gdx.graphics.getWidth() / 3); // drawing Game Over
batch.end();
stage.draw(); // drawing restart Button
}
【问题讨论】: