Libgdx MainMenu没有回复

时间:2016-06-17 13:44:21

标签: java libgdx

我定义了一个mainmenu类,我试图在表格中显示一些图像,并让它们响应触摸,但是当我在主屏幕的构造函数中定义调用类(只是调用它)时,控件不要&#虽然我以定义控件类的相同方式定义了mainmenu类,但我没有回应。 (注意当我在主屏幕内使用mainmenu对象时,它会渲染图像和控件,但它们不会响应任何点击)。 当我打电话给主菜单时,主要问题就出现了,游戏正在运行,但控件不会响应触摸。这是两个类的代码:

public class Controller {
    public Viewport viewport;
    public Stage stage;
    public boolean upPressed;
    public boolean leftPressed;
    public boolean rightPressed;
    public boolean pausePressed;
    public static boolean visiblity;
    public Image buttonUp;
    public Image buttonDown;
    public Image buttonLeft;
    public Image buttonRight;
    public Image buttonpause;
    public OrthographicCamera camera;
    public Table table;
    public Table table1;
    public Table table2;

    //Constructor.
    public Controller(SpriteBatch spriteBatch) {
        camera = new OrthographicCamera();
        viewport = new FitViewport(Fruits.V_WIDTH, Fruits.V_HIEGT, camera);
        stage = new Stage(viewport, spriteBatch);
        Gdx.input.setInputProcessor(stage);
        visiblity=false;
        //Buttons with images.
        buttonUp = new Image(new Texture("Pause.png"));
        buttonUp.setSize(30, 30);

        buttonUp.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                upPressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                upPressed = false;
            }
        });

        buttonLeft = new Image(new Texture("buttonleft.png"));
        buttonLeft.setSize(65, 65);
        buttonLeft.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                leftPressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                leftPressed = false;
            }
        });
        buttonRight = new Image(new Texture("buttonright.png"));
        buttonRight.setSize(65, 65);
        buttonRight.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                rightPressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                rightPressed = false;
            }
        });
        buttonpause = new Image(new Texture("pausing.png"));
        buttonpause.setSize(30, 30);

        buttonpause.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                pausePressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                pausePressed = false;
            }
        });

        //Table with buttons.

        table = new Table();
        table.left().bottom(); //Align to the left bottom.
        table.add(buttonLeft).size(buttonLeft.getWidth(), buttonLeft.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
       // table.add(buttonUp).size(buttonUp.getWidth(), buttonUp.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
        table.add(buttonRight).size(buttonRight.getWidth(), buttonRight.getHeight());

        stage.addActor(table);
        table1=new Table();
        table1.right().top();
        table1.setFillParent(true);
        //table1.add();
        table1.row().pad(25, 3, 0, 10);
        table1.add(buttonUp).size(buttonUp.getWidth(), buttonUp.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
        stage.addActor(table1);

        table2=new Table();
        table2.center();
        table2.setFillParent(true);
        //table1.add();
        table2.row().pad(25, 3, 0, 10);
        table2.add(buttonpause).size(buttonpause.getWidth(), buttonpause.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);

        stage.addActor(table2);

    }
    public void draw() {
        stage.draw();
    }


    public void resize(int width, int height) {
        viewport.update(width, height);
    }
    public boolean isUpPressed() {
        return upPressed;
    }

    public boolean isLeftPressed() {
        return leftPressed;
    }

    public boolean isRightPressed() {
        return rightPressed;
    }
    public boolean isPausePressed() {
        return pausePressed;
    }

}
mainmenu的代码

public class MainMenu{
  /*  public Viewport viewport;
    public Stage stage;
    public boolean pausePressed;
    public boolean resumePressed;
    public boolean exitPressed;
    public Image buttonpause;
    public Image buttonDown;
    public Image buttonresume;
    public Image buttonexit;
    public OrthographicCamera camera;
    public Table table;
    public Table table2;

    //Constructor.
    public MainMenu(SpriteBatch spriteBatch) {
        camera = new OrthographicCamera();
        viewport = new FitViewport(Fruits.V_WIDTH, Fruits.V_HIEGT, camera);
        stage = new Stage(viewport, spriteBatch);
        Gdx.input.setInputProcessor(stage);

        //Buttons with images.
        buttonpause = new Image(new Texture("pausing.png"));
        buttonpause.setSize(30, 30);

        buttonpause.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                pausePressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                pausePressed = false;
            }
        });

        buttonresume = new Image(new Texture("resume.png"));
        buttonresume.setSize(65, 65);
        buttonresume.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                resumePressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                resumePressed = false;
            }
        });
        buttonexit = new Image(new Texture("exit.png"));
        buttonexit.setSize(65, 65);
        buttonexit.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                exitPressed = true;
                return true;
            }

            @Override
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
                exitPressed = false;
            }
        });

        //Table with buttons.
        table2=new Table();
        table2.center();
        table2.setFillParent(true);
        //table2.add();
        table2.row().pad(25, 3, 0, 10);
        table2.add(buttonpause).size(buttonpause.getWidth(), buttonpause.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
        table2.row().pad(25, 3, 0, 10);
        table2.add(buttonresume).size(buttonresume.getWidth(), buttonresume.getHeight());
        table2.row().pad(25, 3, 0, 10);
        table2.add(buttonexit).size(buttonexit.getWidth(), buttonexit.getHeight()).spaceRight((viewport.getWorldWidth() / 10) * 6.5f);
        stage.addActor(table2);

    }
    public void draw() {
        stage.draw();

    }

    public void resize(int width, int height) {
        viewport.update(width, height);
    }
    public boolean isPausePressed() {
        return pausePressed;
    }

    public boolean isResumePressed() {
        return resumePressed;
    }

    public boolean isExitPressed() {
        return exitPressed;
    }
*/
}

1 个答案:

答案 0 :(得分:0)

一次只能设置一个输入处理器,因此只有最近一次对Gdx.input.setInputProcessor的调用才有效。因此,将该调用移至屏幕的show()方法(如果您正在实施Screen,否则请执行您的结构中的等效操作)。

如果您希望它们能够同时工作,您还可以使用InputMultiplexer将多个输入处理器合并为一个。