RadioButton and RadioButtonGroup

RadioButton and RadioButtonGroup
0.0 0

#1

How do I create RadioButtons and Group them together in a RadioButtonGroup
I tried but for some reason the radio buttons aren’t visible can anyone give me an example of how to achieve this?


#2

I’m new to cocos2d-x, but let’s try to help.
I tried this on Cocos2d-x v3.17

Put this code in the init() of your scene and don’t forget the header include

#include “ui/CocosGUI.h”

bool HelloWorld::init()
{

// a radio button group
auto radioGroup = ui::RadioButtonGroup::create();

    auto btn1 = ui::RadioButton::create("check_box_normal.png",
                                        "check_box_normal_press.png",
                                        "check_box_active.png",
                                        "check_box_normal_disable.png",
                                        "check_box_active_disable.png");
    btn1->setPosition(Vec2(50.f, 100.f));

    auto btn2 = ui::RadioButton::create("check_box_normal.png",
                                        "check_box_normal_press.png",
                                        "check_box_active.png",
                                        "check_box_normal_disable.png",
                                        "check_box_active_disable.png");
    btn2->setPosition(Vec2(50.f, 150.f));

    auto btn3 = ui::RadioButton::create("radio_button_off.png", "radio_button_on.png");
    btn3->setPosition(Vec2(50.f, 200.f));

    radioGroup->addChild(btn1);
    radioGroup->addChild(btn2);
    radioGroup->addChild(btn3);
    radioGroup->addRadioButton(btn1);
    radioGroup->addRadioButton(btn2);
    radioGroup->addRadioButton(btn3);

    addChild(radioGroup);


return true;
}

The used pictures can be found in the cocos2d-x/tests/cpp-tests/Resources/ccs-res/cocosui/
The first and the second buttons have different pictures for the diff states, when selected, pressed and selected and so on. And the third is a radio button (on/off) which seems the usual usage.


#3

I believe there is a working example in cpp-tests.