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

标签 java libgdx

所以我的问题是,当游戏结束时,游戏会进入一个新屏幕(称为“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/

相关文章:

java - Java 计时器领域的现状如何?

java - 奇怪的分割方法

java - 我的应用程序在执行 Intent 语句时崩溃

Java//数组//乘法//For循环;

unit-testing - 有没有办法为 libGDX 应用程序创建集成测试?

java - 如何将碰撞盒置于旋转三角形 Sprite 内的中心?

java - 如何在Java中获取圆中相交部分的大小

java - 是否可以重用包含闭包的 Runnable

java - Libgdx 如何在 Action 后执行代码?

java - (LibGDX And​​roid) 无法实例化 Activity ComponentInfo ClassNotFoundException