简体   繁体   English

Java重新启动游戏不起作用(libGDX中的屏幕切换)

[英]Java Restarting Game doesn't work (screen switch in libGDX)

纽扣

In my GameOverScreen class I have the following method (which renders a Restart Button and should create a new game): 在我的GameOverScreen类中,我具有以下方法(呈现“重新启动按钮”并应创建一个新游戏):

@Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.42f, 0.42f, 0.7f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        game.batch.begin();
        game.batch.draw(AssetManager.gameover,0,0);

        if (Gdx.input.getX() < x + WIDTH + 100 && Gdx.input.getX() > x + 50
                && Gdx.input.getY() < HEIGHT + 150
                && Gdx.input.getY() > HEIGHT + 50)
        {
            game.batch.draw(AssetManager.PlayActive, camera.viewportWidth / 2, camera.viewportHeight - 200, WIDTH, HEIGHT);
            if (Gdx.input.isTouched()){
                game.setScreen(new GameScreen(game));
            }
        } else {
            game.batch.draw(AssetManager.PlayInactive, camera.viewportWidth / 2, camera.viewportHeight - 200, WIDTH, HEIGHT);
        }
}

The problem is that when I click on "Play", it doesn't create a new game. 问题是,当我单击“播放”时,它不会创建新游戏。 Instead, it constantly switches the screens (to the game that I played before and to the GameOverScreen .) 而是不断地切换屏幕(切换到我之前玩过的游戏以及GameOverScreen 。)

I don't know what I have to change to create a new game after the old one is over. 在旧游戏结束后,我不知道要更改什么才能创建新游戏。 If you want to see the code of the GameScreen class, please comment. 如果您想查看GameScreen类的代码,请发表评论。 I don't know if you need it. 不知道你是否需要

It might have something to do with this: 它可能与此有关:

public class GameScreen implements Screen {

final FlappyWizardGame game;
OrthographicCamera camera;
private GameWorld world;
private GameRenderer renderer;
private float runTime = 0; 
private float timer = 4.1f;

public GameScreen(FlappyWizardGame game) {
    this.game = game;

    camera = new OrthographicCamera();
    camera.setToOrtho(false, 1280, 720);

    float screenWidth = Gdx.graphics.getWidth();
    float screenHeight = Gdx.graphics.getHeight();
    float gameWidth = 136;
    float gameHeight = screenHeight / (screenWidth / gameWidth);
    int midPointY = (int) (gameHeight / 2);

    Gdx.app.log("GameScreen", "Attached");
    world = new GameWorld(midPointY);
    renderer = new GameRenderer(world, (int) gameHeight, midPointY);

    Gdx.input.setInputProcessor(new InputHandler(world.getWizard()));
}

Here the constructor of the GameOverScreen 这里是GameOverScreen的构造GameOverScreen

public class GameOverScreen extends GameScreen implements Screen {

protected OrthographicCamera camera;
private String name = "TEST";
private int finalScore = GameWorld.score;
private List<Highscore> highscores;

private TextButton save;
private Stage stage;

private TextField nureintest;

private static final int WIDTH = 280;
private static final int HEIGHT = 80;

private float x = (1600 / 2 -  WIDTH / 2);


public GameOverScreen(FlappyWizardGame game) {
    super(game);
    camera = new OrthographicCamera();
    camera.setToOrtho(false,1280,720);

    stage = new Stage(new ScreenViewport());
    Gdx.input.setInputProcessor(stage);
    // ....
}

I forgot to set fuerGameoverScreen to false . 我忘记将fuerGameoverScreen设置为false

Simple as that. 就那么简单。

Thanks to Morchul. 感谢Morchul。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM