LIBGDX:FirstPersonCameraController和TextButton一起工作

时间:2015-08-02 07:47:02

标签: java libgdx

我正在编写一个新的3D游戏并使用FirstPersonCameraController(子类),效果很好。然后我检测我是否在移动设备上并向舞台添加了一些TextButton。这些出现但无法按下?

public ControlsController(Camera camera, GameRenderScreen gameRenderScreen, Stage stage) {
        super(camera);
        this.gameRenderScreen = gameRenderScreen;

        // create control
        if (Gdx.app.getType() == Application.ApplicationType.Android || Gdx.app.getType() == Application.ApplicationType.iOS)
        {
            forwardButton = new TextButton("Forward", Gui.getSkin());
            forwardButton.setBounds(width() - 130,  130, 80, 50);
            stage.addActor(forwardButton);

            leftButton = new TextButton("Left", Gui.getSkin());
            leftButton.setBounds(width() - 170,  70, 70, 50);
            stage.addActor(leftButton);

            rightButton = new TextButton("Right", Gui.getSkin());
            rightButton.setBounds(width() - 80,  70, 70, 50);
            stage.addActor(rightButton);

            backwardButton = new TextButton("Backward", Gui.getSkin());
            backwardButton.setBounds(width() - 130,  10, 80, 50);
            stage.addActor(backwardButton);
        }

        InputMultiplexer multiplexer = new InputMultiplexer();
        multiplexer.addProcessor(stage);
        Gdx.input.setInputProcessor(multiplexer);

    }

//这是通过主渲染循环

调用的
public void updateControls(){

        if (Gdx.app.getType() == Application.ApplicationType.Android || Gdx.app.getType() == Application.ApplicationType.iOS) {

            if (forwardButton.isPressed()) {
                keyDown(Input.Keys.W);
            }

            if (backwardButton.isPressed()) {
                keyDown(Input.Keys.S);
            }

            if (leftButton.isPressed()) {
                keyDown(Input.Keys.A);
            }

            if (rightButton.isPressed()) {
                keyDown(Input.Keys.D);
            }
        }

    }

//更新相机

    public void update(){

            fps.update();
            fps.updateControls();
            camera.position.set(camera.position.x, 0.0f, camera.position.z); 
        }

//更新渲染循环

@Override
    public void render(float delta) {

        update();

        Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

        //TODO do we need all of these?
        //Do all your basic OpenGL ES setup to start the screen render.
        Gdx.gl20.glClearColor(0.0f, 0.3f, 0.5f, 1);
        Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        Gdx.gl20.glEnable(GL20.GL_TEXTURE_2D);
        Gdx.gl20.glEnable(GL20.GL_BLEND);
        Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
        Gdx.gl20.glCullFace(GL20.GL_BACK);
        Gdx.gl20.glEnable(GL20.GL_DEPTH_TEST);

        // Like spriteBatch, just with models!  pass in the box Instance and the environment
        modelBatch.begin(camera);

        Skybox.update(camera.position);
        modelBatch.render(Skybox.modelInstance);

        for (int i = 0; i < boxInstance.size; i ++ ) {
            modelBatch.render(boxInstance.get(i));
        }

        modelBatch.end();

        //TODO Whats this for
        stage.getViewport().update(width(), height(), true);
        stage.act(delta);
        stage.draw();

        drawFPS();
    }

1 个答案:

答案 0 :(得分:0)

我总是喜欢发布一个答案,以帮助他人。

我设置了所有按钮后设置了Gdx.input.setInputProcessor(fpsController),我认为这些按钮覆盖了原来的Gdx.input.setInputProcessor(多路复用器);