Java LibGDX:Buttonis在阶段中绘制两次

Java LibGDX:Buttonis在阶段中绘制两次,java,libgdx,Java,Libgdx,所以我的问题是,当一个游戏结束时,它会把游戏扔进一个新的屏幕(称为“结束状态”),在那里我画出了标题“游戏结束”和一个名为“重新启动”的图像按钮,所以背景、玩家和其他一切都与前一个屏幕相同,但冻结了。当我点击我的重启按钮时,它会在下方移动10px(按下效果,使用pressedOffsetY实现),但我看到了它后面的同一个按钮,我发现解决这个问题的唯一方法是在用这个按钮绘制舞台之前,在屏幕上画满东西,但这样一来,我就失去了我的球员形象和其他一切从上一个屏幕 我使用Stage(因为它是可点击的)以这

所以我的问题是,当一个游戏结束时,它会把游戏扔进一个新的屏幕(称为“结束状态”),在那里我画出了标题“游戏结束”和一个名为“重新启动”的图像按钮,所以背景、玩家和其他一切都与前一个屏幕相同,但冻结了。当我点击我的重启按钮时,它会在下方移动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
    }

我相信基本的问题是你没有在每次渲染时清除屏幕(故意这样做,但这也会导致你的问题)

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

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


正确的解决方案:既然你想保留旧的图形,就不要制作新的屏幕,只需在上一个屏幕中插入你的按钮演员,然后从那里开始工作

是的,问题完全如你所说!您的解决方案肯定会奏效,非常感谢!但我有点“作弊”——我创建了一个新的图像,完全用黑色填充,比我按钮的图像大一点,我把它放在按钮的正后方,这样它就可以覆盖旧的渲染图像。我还尝试了您提出的第二种解决方案,但我的PlayState类有点混乱,我不希望它变得更麻烦。无论如何,非常感谢你!