【问题标题】:LibGDX: Buttonis is drawn twice in stageLibGDX:Buttonis 在舞台上绘制了两次
【发布时间】: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
    }

【问题讨论】:

    标签: java libgdx


    【解决方案1】:

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

    由于您没有清除屏幕,因此您的最后一个屏幕仍然可见,但在此屏幕上绘制的所有内容也将可见(测试移动按钮比您已经移动的更多,您将开始在任何地方看到它的副本移动它)。

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

    正确的解决方案:既然您希望旧图形保留下来,请不要创建新屏幕,只需将您的按钮演员插入前一个屏幕并从那里开始工作

    【讨论】:

    • 是的,问题就是你说的!您的解决方案肯定会奏效,非常感谢!但我“作弊”了一点——我创建了一个完全用黑色填充的新图像,比我的按钮图像大一点,我把它放在按钮后面,这样它就可以覆盖旧的渲染图像。我也尝试了您提出的第二种解决方案,但是我的 PlayState 类有点乱,我不希望它变得更麻烦。无论如何,非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 2013-09-14
    • 2013-04-04
    • 2012-12-11
    • 1970-01-01
    相关资源
    最近更新 更多