LibGDX: Make all actors on a stage unchecked

自闭症网瘾萝莉.ら 提交于 2019-12-10 19:52:34

问题


I have a stage with multiple buttons on it that basically serves as a toolbox. I want the user to be able to select between the different items that are displayed; therefore when the user selects one item, all others have to be deselected.

I thought of doing that with the checked property of libGDX buttons. However, I don't know how to programatically uncheck a button and to acces all actors on a stage in the simplest possible way.

I can't provide code becuase as I said, I don't even know how tp uncheck a button and google doesn't help. Is that even possible? If not, I'd be happy about other suggestions.


回答1:


Take a look at a ButtonGroup

ButtonGroup is not an actor and has no visuals. Buttons are added to it and it enforce a minimum and maximum number of checked buttons. This allows for buttons (button, text button, checkbox, etc) to be used as "radio" buttons. https://github.com/libgdx/libgdx/wiki/Scene2d.ui#wiki-ButtonGroup

Also try and take a look at the useful javadocs for it http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/scenes/scene2d/ui/ButtonGroup.html

Basically you create your ButtonGroup add actors and set a minimum amount of checked things that should be allowed.

//initalize stage and all your buttons
ButtonGroup buttonGroup = new ButtonGroup(button1, button2, button3, etc...)
//next set the max and min amount to be checked
buttonGroup.setMaxCheckCount(1);
buttonGroup.setMinCheckCount(0);
//it may be useful to use this method:
setUncheckLast(true); //If true, when the maximum number of buttons are checked and an additional button is checked, the last button to be checked is unchecked so that the maximum is not exceeded.


来源:https://stackoverflow.com/questions/21636321/libgdx-make-all-actors-on-a-stage-unchecked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!