辅助屏幕上的libGDX 1.4.1按钮监听器

时间:2014-11-13 00:23:16

标签: libgdx

我有两个屏幕,我的核心文件夹中有一个MainActivityrender()方法分为几个switch个案例。在游戏结束时,switch案例的这一部分会被触发,它会调用我的游戏类的渲染部分:

         case STOPPED:

                // Exit and clean the game

                gameOverScreen.render(Gdx.graphics.getDeltaTime());


                break;
                ...

这是我的GameOverScreen类:

public class GameOverScreen implements Screen {

    private SpriteBatch gameOverBatch;
    private FreeTypeFontGenerator gameOverFontGen;
    private FreeTypeFontGenerator.FreeTypeFontParameter gameOverLogoParam;
    private FreeTypeFontGenerator.FreeTypeFontParameter gameOverButtonParam;
    private final MainActivity mainActivity;
    private OrthographicCamera camera;
    private BitmapFont bitmapLogoFont;
    private BitmapFont bitmapButtonFont;
    private Button exitGameButton;
    private Button gameRestartButton;
    private Sound buttonSound;
    private Stage stage;
    private float w;
    private float h;

    // Constructor
    public GameOverScreen(MainActivity mainActivity) {

        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();

        gameOverBatch = new SpriteBatch();

        this.mainActivity = mainActivity;
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 300, 300);

        // Instantiate the font for this screen from file
        gameOverFontGen = new FreeTypeFontGenerator(Gdx.files.internal("fonts/SF_Wonder_Comic.ttf"));
        gameOverLogoParam = new FreeTypeFontGenerator.FreeTypeFontParameter();
        gameOverLogoParam.size = 110;

        gameOverButtonParam = new FreeTypeFontGenerator.FreeTypeFontParameter();
        gameOverButtonParam.size = 40;

        // Font for the logo
        bitmapLogoFont = new BitmapFont();
        bitmapLogoFont = gameOverFontGen.generateFont(gameOverLogoParam);


        // Font for the buttons
        bitmapButtonFont = new BitmapFont();
        bitmapButtonFont = gameOverFontGen.generateFont(gameOverButtonParam);

        // Instantiate the buttonSound
        buttonSound = Gdx.audio.newSound(Gdx.files.internal("sounds/pauseBtn_sound.ogg"));

/*************************************** Create a Stage *******************************************/
        stage = new Stage();


        // Add some actors as the buttons
        exitGameButton = new Button(new TextureRegionDrawable(
                new TextureRegion(new Texture(Gdx.files.internal("images/off_red.png")))),
                new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("images/off_white.png")))));
        //exitGameButton.setX((w, 150));
        //pauseButton.setY(flipCoordinates(h, 150));

        exitGameButton.setOrigin(exitGameButton.getWidth() / 2, exitGameButton.getHeight() / 2);
        exitGameButton.setBounds(w / 2 + 100, h / 2 - 60, exitGameButton.getWidth(), exitGameButton.getHeight());

        exitGameButton.act(Gdx.graphics.getDeltaTime());

        stage.addActor(exitGameButton);

        exitGameButton.addListener(new ChangeListener() {
            @Override
            public void changed (ChangeEvent event, Actor actor) {

                buttonSound.play();
            }
        });

        // Add some actors as the buttons
        TextButton.TextButtonStyle restartStyle = new TextButton.TextButtonStyle();
        restartStyle.font = bitmapButtonFont;
        restartStyle.up = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("images/back_red.png"))));
        restartStyle.down = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("images/back_white.png"))));
        gameRestartButton = new Button(new TextButton.TextButtonStyle(restartStyle));

        gameRestartButton.setOrigin(gameRestartButton.getWidth() / 2, gameRestartButton.getHeight() / 2);
        gameRestartButton.setBounds(w / 2 - 100, h / 2 - 60, gameRestartButton.getWidth(), gameRestartButton.getHeight());

        gameRestartButton.act(Gdx.graphics.getDeltaTime());
        stage.addActor(gameRestartButton);

        // Capture the event listener for the return button
        gameRestartButton.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {

                // Do something on restart
                //MainActivity.state = MainActivity.State.RUN;
                Gdx.app.log("GameRestartButton", " has been pressed");
            }
        });

        gameRestartButton.act(Gdx.graphics.getDeltaTime());

        //stage.addActor(exitGameButton);
        //stage.addActor(gameRestartButton);

    }

    @Override
    public void render(float delta) {

        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();

        gameOverBatch.begin();
        bitmapLogoFont.setColor(Color.RED);
        bitmapLogoFont.draw(gameOverBatch, "Game Over", w / 2 - 210, h / 2 + 150f);
        gameOverBatch.end();

        exitGameButton.act(Gdx.graphics.getDeltaTime());

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

    }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {
        gameOverFontGen.dispose();

    }
}

在GameOverScreen构造函数中,我有两个按钮(exitGameButtongameReturnButton)我希望能够触发他们的事件监听器,但我似乎无法做到这一点。我的按钮都没有响应事件监听器。我究竟做错了什么?我是否需要在我的MainActivity课程中添加其他内容才能使这些按钮生效?非常感谢。

2 个答案:

答案 0 :(得分:0)

我想通了,在第二个屏幕上我从构造函数中遗漏了这个:Gdx.input.setInputProcessor(stage);,当然,只有先前创建了一个舞台才有效。

答案 1 :(得分:0)

在你的GameOverScreen类中试试这个

.//
public Stage getStageGameOverScreen(){
return this.stage;
}

你的案子:

case STOPPED:

            // Exit and clean the game

            Gdx.input.setInputProcessor(gameOverScreen.getStageGameOverScreen());
            gameOverScreen.render(Gdx.graphics.getDeltaTime());


            break;
            ...