How to properly create a mouse Listener for CheckBox in LibGDX Scene2D?

时间:2016-03-27 14:48:58

标签: java checkbox libgdx listener scene2d

I've been searching online for hours and I'm new to LibGDX game development. This link was useful, but I still have not been able make my CheckBox work. How to properly implement CheckBox in LibGDX

我正在使用LibGDX,我必须在菜单中实现两个单选按钮。

我创建了两个CheckBox并为它们分配了一个样式。

    check_style = new CheckBox.CheckBoxStyle();
    check_style.font = font;
    check_style.fontColor = new Color(Color.WHITE);
    check_style.checkboxOff = check_skin.getDrawable("checkbox");
    check_style.checkboxOn = check_skin.getDrawable("checkbox2");
    check_style.checked = check_skin.getDrawable("checkbox2");

我还添加了一个监听器来注意鼠标点击

    controls1Check.addListener(new InputListener() {
        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
            controls1Check.toggle();
            System.out.println("toggle");
        }
        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
           System.out.println("Controls1: " + controls1Check.isChecked());
           return true;
        };
    });

当我运行程序时,它不响应任何鼠标点击。 我该如何解决这个问题?

谢谢!

1 个答案:

答案 0 :(得分:0)

我猜您要将CheckBox添加到Stage?如果是这样,我唯一的想法就是你可能没有调用Gdx.input.setInputProcessor(yourStage);所以即使你的CheckBox有InputListeners,实际上也没有处理输入。