Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2dx用checkbox实现单选框和button实现table按钮大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

转载请注明出处:帘卷西风的专栏(http://blog.csdn.net/ljxfblog)

cocos2dx有checkbox和button,但是checkbox是个复选框,也没有table按钮,本文主要是利用这两个控件来实现单选框和table按钮的功能。

主要思路就是,通过响应checkbox和button的事件,来设置和他一组的其他控件的状态来达到我们需要的效果。

我的工作环境时cocos2dx3.2+lua。

首先看看checkbox的实现,我用来实现男女性别的选择。

local mancheck = self:getChild("checkBox_Man")
local womancheck = self:getChild("checkBox_Woman")
if mancheck and womancheck then
	local function callBACk(sender,eventTypE)
		if eventType == ccui.checkBoxEventType.SELEcted then
			if sender == mancheck then
				womancheck:setSELEctedState(false)
			else					
				mancheck:setSELEctedState(false)
			end
		elseif eventType == ccui.checkBoxEventType.unSELEcted then
			if sender == mancheck then
				womancheck:setSELEctedState(true)
			else	
				mancheck:setSELEctedState(true)		
			end
		end
	end
	mancheck:addEventListener(callBACk)
	womancheck:addEventListener(callBACk)
	mancheck:setSELEctedState(true)
	womancheck:setSELEctedState(false)
end	

然后再来一个table按钮的实现。

先定义一些table的常量定义:

--定义常量
local Item_Tag_All        = 1000        
local Item_Tag_Equip      = 1001
local Item_Tag_Material   = 1002
local Item_Tag_Other      = 1003

local ButtonSwitch = 
{
	[Item_Tag_All] = "全部",[Item_Tag_Equip] = "装备",[Item_Tag_Material] = "材料",[Item_Tag_Other] = "其他",}
然后创建按钮,并设置tag:
--筛选按钮
local width = title_bg:getContentSize().width
for tag = Item_Tag_All,Item_Tag_Other do
	if ButtonSwitch[tag] then
		local curbtn = ccui.button:create()
		curbtn:setTouchEnabled(true)
		curbtn:setScale9Enabled(true)
		curbtn:loadTextures(BTN__NORMAL,BTN_SELECTED,"",ccui.TextureResType.plistTypE)
		curbtn:setSize(cc.size(100,53))
		local size = curbtn:getContentSize()
		curbtn:setPosition(cc.p(width + size.width / 2,winSize.height - 20 - size.height / 2))
		curbtn:settitleText(ButtonSwitch[tag])
		curbtn:settitleFontSize(25)
		curbtn:setTag(tag)
		self._widget:addChild(curbtn)

		--注册点击事件
		local function callBACk_tag(sender,eventTypE)
			if eventType == ccui.TouchEventType.ended then
				showTable(tag)
			end
		end
		curbtn:addTouchEventListener(callBACk_tag)
		width = width + curbtn:getContentSize().width + 10
	end
end
最后是显示按钮的规则,把同组的其他table设置成正常,选中的设置成高亮:

--显示一个table
function 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
    end
end

好了,分享完了。看看效果图吧!

大佬总结

以上是大佬教程为你收集整理的cocos2dx用checkbox实现单选框和button实现table按钮全部内容,希望文章能够帮你解决cocos2dx用checkbox实现单选框和button实现table按钮所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。