Cocos2dx using a checkbox to implement a table button with a radio box and button

Source: Internet
Author: User

Reprint please indicate the source: The Curtain Roll westerly column (Http://blog.csdn.net/ljxfblog)

The COCOS2DX has a checkbox and a button, but the checkbox is a check box and there is no table button, this article mainly uses these two controls to implement the function of the radio box and the Table button.

The main idea is to set the state of the other controls in a group to achieve the effect we need by responding to the checkbox and button events.

My working environment is Cocos2dx3.2+lua.

First look at the implementation of the checkbox, which I use to achieve male and female choices.

Local Mancheck = Self:getchild ("Checkbox_man") Local Womancheck = Self:getchild ("Checkbox_woman") if Mancheck and Womancheck thenlocal function Callback (sender, EventType) if EventType = = Ccui. checkboxeventtype.selected thenif Sender = = Mancheck Thenwomancheck:setselectedstate (false) Elsemancheck: Setselectedstate (false) endelseif EventType = = Ccui. checkboxeventtype.unselected thenif Sender = = Mancheck Thenwomancheck:setselectedstate (true) Elsemancheck: Setselectedstate (True) Endendendmancheck:addeventlistener (callback) Womancheck:addeventlistener (callback) Mancheck:setselectedstate (True) Womancheck:setselectedstate (false) end

then come to the implementation of a table button.

Define some of the table's constant definitions first:

--Define constants local Item_tag_all        =        Item_tag_equip      = 1001local item_tag_material   = 1002local item_ Tag_other      = 1003local Buttonswitch = {[Item_tag_all] = "All", [item_tag_equip] = "Equip", [item_tag_material] = "Material", [Item _tag_other] = "Other",}
then create the button and set tag:

--Filter button Local width = title_bg:getcontentsize (). widthfor tag = item_tag_all, Item_tag_other doif buttonswitch[tag] thenlocal curbtn = c Cui. Button:create () curbtn:settouchenabled (True) curbtn:setscale9enabled (True) curbtn:loadtextures (BTN__NORMAL, BTN_ SELECTED, "", Ccui. Texturerestype.plisttype) Curbtn:setsize (cc.size (+)) Local size = Curbtn:getcontentsize () curbtn:setposition ( CC.P (width + size.width/2, WINSIZE.HEIGHT-20-SIZE.HEIGHT/2)) Curbtn:settitletext (Buttonswitch[tag]) curbtn:setTitle FontSize (+) Curbtn:settag (tag) self._widget:addchild (CURBTN)--register Click event Local function Callback_tag (sender, EventType) if EventType = = Ccui. toucheventtype.ended thenshowtable (tag) endendcurbtn:addtoucheventlistener (callback_tag) width = width + curbtn: Getcontentsize (). Width + 10endend 
Finally, the rule that displays the button, the other table in the same group is set to normal, the selected setting is highlighted:

--Displays a tablefunction showtable (Showtag)    for tag = Item_tag_all, item_tag_other do        local tagbar = Self._widget: Getchildbytag (TAG)        if Tagbar then            if Showtag = = Tag then                tagbar:setbrightstyle (ccui. Brightstyle.highlight)            Else                            Tagbar:setbrightstyle (ccui. Brightstyle.normal)            End        End    EndEnd

All right, I'm done sharing. Look at it!

Cocos2dx using a checkbox to implement a table button with a radio box and button

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.