cocos2dx 3.x (单选,多选,复选checkBox按钮的实现) RadioButton

 1 //
 2 //  LandLordsMakeNewRoom.hpp
 3 //  MalaGame39
 4 //
 5 //  Created by work on 2016/12/19.
 6 //
 7 //
 8  
 9 #ifndef LandLordsMakeNewRoom_hpp
10 #define LandLordsMakeNewRoom_hpp
11  
12 #include <stdio.h>
13 #include <cocos2d.h>
14 #include "ui/cocosGUI.h"
15 #include <cocostudio/CocoStudio.h>
16 using namespace cocos2d::ui;
17  
18 class LandLordsMakeNewRoom : public cocos2d::Layer
19 {
20 private:
21     cocos2d::Node* m_rootNode;
22     Button* m_return;
23 //    long long m_ruleNum;
24     RadioButton* m_boomButton[4];
25     RadioButton* m_numberButton[3];
26     int m_ruleNum[6] = {0,0,0,0,0,0};
27 public:
28     bool init();
29     void onEnter();
30     void onExit();
31     void buttonReturnCallback(Ref *senderz, cocos2d::extension::Control::EventType controlEvent);
32 //    void onChangedRadioButtonOneSelect(RadioButton* radioButton, RadioButton::EventType type);
33 //    void onChangedRadioButtonTwoSelect(RadioButton* radioButton, RadioButton::EventType type);
34 //    void onChangedRadioButtonThreeSelect(RadioButton* radioButton, RadioButton::EventType type);
35 //    void onChangedRadioButtonFourSelect(RadioButton* radioButton, RadioButton::EventType type);
36     void buttonMakeNewRoomCallback(Ref *senderz, cocos2d::extension::Control::EventType controlEvent);
37     void onChangedRadioButtonBoomBgSelect(RadioButton* radioButton, RadioButton::EventType type);
38     void onChangedRadioButtonChooseBgSelect(RadioButton* radioButton, RadioButton::EventType type);
39     void setLayerData(GameRuleData& rule);
40     void checkBoxCallback(CheckBox* sender,CheckBoxEventType type);
41     CREATE_FUNC(LandLordsMakeNewRoom);
42 };
43  
44  
45 #endif /* LandLordsMakeNewRoom_hpp */
46  
47  
  1 //
  2 //  LandLordsMakeNewRoom.cpp
  3 //  MalaGame39
  4 //
  5 //  Created by work on 2016/12/19.
  6 //
  7 //
  8  
  9 #include "LandLordsMakeNewRoom.hpp"
 10 #include "cocos-ext.h"
 11 #include <stdio.h>
 12 USING_NS_CC;
 13 using namespace cocos2d::ui;
 14 using namespace cocos2d::extension;
 15 bool LandLordsMakeNewRoom::init()
 16 {
 17     if (!Layer::init()) {
 18         return false;
 19     }
 20     
 21     Sprite* bg = Sprite::create("LandLord/LandLordsmake_room_bg.png");//背景图片
 22     bg->setPosition(Vec2(612,372));
 23     this->addChild(bg);
 24     
 25     
 26     auto Relieve=cocos2d::ui::Scale9Sprite::createWithSpriteFrameName("friend_fight_x.png");
 27     auto m_buttonRelieve = ControlButton::create(Relieve);
 28     m_buttonRelieve->setPosition( 991.5,644.8);
 29     m_buttonRelieve->addTargetWithActionForControlEvents(this, cccontrol_selector(LandLordsMakeNewRoom::buttonReturnCallback), Control::EventType::TOUCH_UP_INSIDE);//按钮点击事件
 30     this->addChild(m_buttonRelieve);
 31     
 32     
 33     auto share=cocos2d::ui::Scale9Sprite::createWithSpriteFrameName("make_btn.png");
 34     auto m_buttonShare = ControlButton::create(share);
 35     m_buttonShare->setPosition( 627,117);
 36     m_buttonShare->addTargetWithActionForControlEvents(this, cccontrol_selector(LandLordsMakeNewRoom::buttonMakeNewRoomCallback), Control::EventType::TOUCH_UP_INSIDE);
 37     this->addChild(m_buttonShare);
 38  
 39     
 40     CheckBox* checkBox[4] = {nullptr};
 41     for (int i=0;i<4;i++){
 42         //for循环创建4个复选按钮
 43         checkBox[i] = CheckBox::create("LandLord/make_room_unselected.png",//未选中时的显示图片
 44                                        "LandLord/make_room_unselected.png",//点击checkBox后触摸没有松开时的图片
 45                                        "LandLord/make_room_select.png",//选中时的显示图片
 46                                        "LandLord/make_room_select.png",//从选中到未选中切换过程中,触摸没有松开时的显示图片
 47                                        "LandLord/make_room_select.png");
 48         
 49          checkBox[i]->::LandLordsMakeNewRoom::setPosition(402,500-(i*40));
 50          checkBox[i]->setTag(i+1);
 51          checkBox[i]->addEventListenerCheckBox(this ,checkboxselectedeventselector(LandLordsMakeNewRoom::checkBoxCallback));
 52          this->addChild(checkBox[i]);
 53         
 54     }
 55     
 56     
 57  
 58     for (int i = 0; i<4; i++) {//单选(按钮)
 59         m_boomButton[i] = RadioButton::create("make_room_unselected.png", "make_room_select.png", Widget::TextureResType::PLIST);
 60         m_boomButton[i]->setPosition(Vec2(500+(106*i), 330));
 61         m_boomButton[i]->setTag(i+11);
 62         m_boomButton[i]->addEventListener(CC_CALLBACK_2(LandLordsMakeNewRoom::onChangedRadioButtonBoomBgSelect,this));
 63         this->addChild(m_boomButton[i]);
 64     }
 65  
 66     
 67     
 68     int number[3] = {6,10,20};//局数单选
 69     for (int i = 0; i<3; i++) {
 70         m_numberButton[i] = RadioButton::create("make_room_unselected.png", "make_room_select.png", Widget::TextureResType::PLIST);
 71         m_numberButton[i]->setPosition(Vec2(450+200*i+20, 270));
 72         m_numberButton[i]->setTag(i+21);
 73         m_numberButton[i]->addEventListener(CC_CALLBACK_2(::LandLordsMakeNewRoom::onChangedRadioButtonChooseBgSelect,this));
 74         this->addChild(m_numberButton[i]);
 75         auto label = Label::createWithSystemFont(StringUtils::format("%d局(房卡x%d)",number[i],i+1), "", 24);
 76         label->setColor(Color3B::YELLOW);
 77         label->setPosition(Vec2(450+200*i+20, 240));
 78         this->addChild(label);
 79     }
 80     
 81     m_boomButton[0]->setSelected(true);//默认第一个单选按钮为选中
 82     m_numberButton[0]->setSelected(true);
 83     
 84     PublicMethod::addLayerTouchShield(this);
 85  
 86     return true;
 87 }
 88 void LandLordsMakeNewRoom::onEnter()
 89 {
 90     Layer::onEnter();
 91 }
 92 void LandLordsMakeNewRoom::onExit()
 93 {
 94     Layer::onExit();
 95 }
 96  
 97 void LandLordsMakeNewRoom::onChangedRadioButtonBoomBgSelect(RadioButton* radioButton, RadioButton::EventType type)
 98 {
 99     
100     if (radioButton == nullptr) {
101         return;
102     }
103     
104     int tag = ((RadioButton*)radioButton)->getTag();//获取按钮当前的tag值
105     
106     for (int i=0; i<4; i++) {//for循环实现单选
107         m_boomButton[i]->setSelected(false);//先让全部按钮设为处于未选中状态
108         if (i+11==tag) {//通过tag值进入判断
109             m_boomButton[i]->setSelected(true);//让当前按钮为选中
110             m_ruleNum[4]=tag-11;
111         }
112     }
113     
114 }
115 void LandLordsMakeNewRoom::onChangedRadioButtonChooseBgSelect(RadioButton* radioButton, RadioButton::EventType type)
116 {
117     if (radioButton == nullptr) {
118         return;
119     }
120     
121     int tag = ((RadioButton*)radioButton)->getTag();
122     
123     for (int i=0; i<3; i++) {
124         m_numberButton[i]->setSelected(false);
125         if (i+21==tag) {
126             m_numberButton[i]->setSelected(true);
127             m_ruleNum[5]=tag-21;
128         }
129     }
130 }
131 
132 //复选按钮的实现方法
133 void LandLordsMakeNewRoom::checkBoxCallback(CheckBox* sender,CheckBoxEventType type)
134 {
135     int tag = sender->getTag();//同样需要拿到当前checkBox的tag值
136     if(type==CHECKBOX_STATE_EVENT_SELECTED){//判断check的状态(选中状态)
137         CCLOG("sender:%d",tag);
138         m_ruleNum[tag-1]=1;
139     //在这里可做其他操作
140     }
141     else if(type==CHECKBOX_STATE_EVENT_UNSELECTED)//为未选中状态
142     {
143         m_ruleNum[tag-1]=0;
144     }
145  
146 }
147 void LandLordsMakeNewRoom::buttonReturnCallback(Ref *senderz, Control::EventType controlEvent)
148 {
149         removeFromParent();//移除当前图层
150     
151 }
原文地址:https://www.cnblogs.com/luorende/p/6206397.html