cocos2dx+lua用checkbox實現單選框和button實現table按鈕

checkbox實現單選框

local ConfigScene = class("ConfigScene", cc.load("mvc").ViewBase)
local checkBoxY = display.cy + 200
local checkBoxX = display.cx - 90
function ConfigScene:onCreate()
	--創建label
    local difficultyChoiceLabel = cc.Label:createWithSystemFont("難度:", "黑體", 20)
    difficultyChoiceLabel:setPosition(checkBoxX, checkBoxY)
    self:addChild(difficultyChoiceLabel)

    local easyLabel = cc.Label:createWithSystemFont("簡單", "黑體", 15)
    easyLabel:setPosition(checkBoxX + 65, checkBoxY)
    self:addChild(easyLabel)

    local normalLabel = cc.Label:createWithSystemFont("普通", "黑體", 15)
    normalLabel:setPosition(checkBoxX + 115, checkBoxY)
    self:addChild(normalLabel)

    local difficultyLabel = cc.Label:createWithSystemFont("困難", "黑體", 15)
    difficultyLabel:setPosition(checkBoxX + 165, checkBoxY)
    self:addChild(difficultyLabel)

	--關鍵部分
    --利用checkBox創建單選框,完成難度選擇功能
    --創建複選框
    local easyCheck = ccui.CheckBox:create()
    easyCheck:setTouchEnabled(true)
    easyCheck:loadTextures("select_normal.png",--未選中狀態
                          "select_selected.png",--選中狀態
                          "select_selected.png",--active狀態,將active狀態設置爲選中狀態的圖片,讓checkBox保持選中狀態
                          "select_normal.png",--normal_disable狀態
                          "select_normal.png")--active_disable狀態
    easyCheck:setPosition(cc.p(checkBoxX + 40, checkBoxY)) --座標
    self:addChild(easyCheck)

    local normalCheck = ccui.CheckBox:create()
    normalCheck:setTouchEnabled(true)
    normalCheck:loadTextures("select_normal.png",--未選中狀態
                          "select_selected.png",--選中狀態
                          "select_selected.png",--active狀態
                          "select_normal.png",--normal_disable狀態
                          "select_normal.png")--active_disable狀態
    normalCheck:setPosition(cc.p(checkBoxX + 90, checkBoxY)) --座標
    self:addChild(normalCheck)

    local difficultCheck = ccui.CheckBox:create()
    difficultCheck:setTouchEnabled(true)
    difficultCheck:loadTextures("select_normal.png",--未選中狀態
                          "select_selected.png",--選中狀態
                          "select_selected.png",--active狀態
                          "select_normal.png",--normal_disable狀態
                          "select_normal.png")--active_disable狀態
    difficultCheck:setPosition(cc.p(checkBoxX + 140, checkBoxY)) --座標
    self:addChild(difficultCheck)

    if easyCheck and normalCheck and difficultCheck then
        local function callback(sender, eventType)
            if eventType == ccui.CheckBoxEventType.selected then
                if  sender == easyCheck then
                    normalCheck:setSelectedState(false)
                    difficultCheck:setSelectedState(false)
                elseif sender == normalCheck then
                    easyCheck:setSelectedState(false)
                    difficultCheck:setSelectedState(false)
                else
                    easyCheck:setSelectedState(false)
                    normalCheck:setSelectedState(false)
                end
            end
        end
        easyCheck:addEventListener(callback)
        normalCheck:addEventListener(callback)
        difficultCheck:addEventListener(callback)
        --設置單選框默認狀態
        easyCheck:setSelectedState(true)
        normalCheck:setSelectedState(false)
        difficultCheck:setSelectedState(false)
    end
end
return ConfigScene

效果圖:
在這裏插入圖片描述

button實現table

local ConfigScene = class("ConfigScene", cc.load("mvc").ViewBase)
--定義常量
local Item_Tag_Easy       = 0       
local Item_Tag_Normal      = 1
local Item_Tag_Difficult   = 2

local ButtonSwitch = 
{
	[Item_Tag_Easy] = "簡單",
	[Item_Tag_Normal] = "普通",
	[Item_Tag_Difficult] = "困難",
}
function ConfigScene:onCreate()
    --使用button創建table
    for tag = Item_Tag_Easy, Item_Tag_Difficult 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(50 + tag * 100, display.cy))
		    curbtn:setTitleText(ButtonSwitch[tag])
		    curbtn:setTitleFontSize(25)
		    curbtn:setTag(tag)
		    self:addChild(curbtn)

		    --註冊點擊事件
		    local function callback_tag(sender, eventType)
			    if eventType == ccui.TouchEventType.ended then
				    self:showTable(tag)
			    end
		    end
		    curbtn:addClickEventListener(callback_tag)
	    end
    end

end
--顯示按鈕的規則,把同組的其他table設置成正常,選中的設置成高亮
--只在點擊時保持高亮,鬆開後回覆normal
function ConfigScene:showTable(tag)
    for tag = Item_Tag_Easy, Item_Tag_Difficult do
        local tagBar = self:getChildByTag(tag)
        if tagBar then
            if showTag == tag then
                tagBar:setBrightStyle(ccui.BrightStyle.highlight)
            else            
                tagBar:setBrightStyle(ccui.BrightStyle.normal)
            end
        end
    end
end
return ConfigScene
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章