简体   繁体   中英

How to properly create a mouse Listener for CheckBox in LibGDX 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

I'm using LibGDX and I have to implement two Radio buttons in my menu.

I created two CheckBox and assigned them a style.

    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");

I also added a listener to notice mouse clicks

    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;
        };
    });

When I run my program it does not respond to any mouse click. How can I resolve this?

Thank you!

I guess you are adding your CheckBox to a Stage ? If so, my only idea is that maybe you didn't call Gdx.input.setInputProcessor(yourStage); so even though your CheckBoxes have InputListeners, input is actually not being processed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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