gpt4 book ai didi

java - LibGDX:Buttonis 在舞台上被绘制了两次

转载 作者:行者123 更新时间:2023-11-30 10:16:16 25 4
gpt4 key购买 nike

所以我的问题是,当游戏结束时,游戏会进入一个新屏幕(称为“EndState”),我在其中绘制标题“Game Over”和一个名为“Restart”的 ImageButton,因此背景和播放器以及所有内容else 与之前的 Screen 相同,但卡住了。当我点击我的重启按钮时,它在下方移动了 10px(按下效果,使用 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
}

最佳答案

我认为基本问题是您没有在每次渲染时都清除屏幕(故意如此,但这也会导致您的问题)。

由于您没有清除屏幕,您的最后一个屏幕仍然可见,但在此屏幕上绘制的所有内容也将可见(测试移动按钮的次数超过您现在的位置,您将开始在任何地方看到它的副本移动它)。

简单的解决方案:不要让任何东西在此屏幕上移动

正确的解决方案:因为您希望保留旧图形,所以不要创建新屏幕,只需将按钮 actor 插入前一个屏幕并从那里开始工作

关于java - LibGDX:Buttonis 在舞台上被绘制了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50151876/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com