cocos2d 封装CheckBox

--标签页代码
    self.tabList = {}
    local func = function (id) 
        for id_, btn in pairs(self.tabList) do
            if checkint(id_) == checkint(id) then
                btn:setSelected(false)
                self:showShopItemsView(id)
            else
                btn:setDefault()
            end
        end
    end
       
    for i=1,2 do
        local btn = require("app.utils.views.common.CheckBoxEx").new("guess_shop_tab1.png", "guess_shop_tab0.png", 0, function ()
            GuessSound.buttonClick()
            func(i)
        end, "horizontal")
        btn:closeRepeatResponse()
        self.tabList[i] = btn
        btn:anchor(.5, 0)
            :addTo(self.innser_bg)
        if i == 1 then
        	btn:pos(self.innser_bg:getContentSize().width*.3, self.innser_bg:getContentSize().height)
        	display.newSprite("#guess_gold.png")
        		:addTo(btn)
        		:pos(btn:getContentSize().width/2, btn:getContentSize().height/2)
        else
        	btn:pos(self.innser_bg:getContentSize().width*.7, self.innser_bg:getContentSize().height)
        	display.newSprite("#guess_zuan_shi.png")
        		:addTo(btn)
        		:pos(btn:getContentSize().width/2, btn:getContentSize().height/2)
        end
        
    end
    self.tabList[1]:setSelected()

--[[自定义CheckBoxEx]]local CheckBoxEx = class("CheckBoxEx",function ()return display.newNode()end)--[[imageParams_:字符串或者九宫{['image']="", ['size']=cc.size(), ['rect']=cc.rect()}imageSelParams_:字符串或者九宫{['image']="", ['size']=cc.size(), ['rect']=cc.rect(), ['border'] = true}border:表示该对象是被选中的外框 isAnimate:是否是动画 animate:动画名字textureResType_:资源类型 0:Plist, 1:本地资源func_:选中回调方法dir_:横向(horizontal) 纵向(vertical)setDark_:设置去色]]function CheckBoxEx:ctor(imageParams_, imageSelParams_, textureResType_, func_, dir_, setDark_, isScale_)self.isScale_ = isScale_self.imageParams_ = imageParams_self.imageSelParams_ = imageSelParams_self.textureResType_ = textureResType_self.func_ = func_self.isClicked = falseself.canClick = falseself.isEnable = trueself.setDark_ = setDark_self.obj_ = nilself.scale_num = 1--是否重复相应选中回调self.repeatResponse = true--是否是checkbox模式(选中、取消状态可以自动切换,否则的话,只能选中)self.isCheckBoxType = falseself.isTouchEnabled = trueself.label = nilself.ttf_size = 20self.enabled_img = nilself.ttf_def_color = cc.c3b(153,85,48)self.ttf_select_color = cc.c3b(255,0,0)local image_ = nilif type(self.imageParams_) == "table" thenimage_ = self.imageParams_.imageif self.imageParams_.rect thenlocal rect_ = self.imageParams_.rectlocal size_ = self.imageParams_.sizeif self.textureResType_ == 0 thenself.obj_ = ccui.Scale9Sprite:createWithSpriteFrameName(image_, rect_)elseself.obj_ = ccui.Scale9Sprite:create(rect_, image_)end self.obj_:anchor(0, 0) :setContentSize(size_) :addTo(self)elseif self.textureResType_ == 0 thenself.obj_ = display.newSprite(string.format("#%s", image_))elseself.obj_ = display.newSprite(image_)endself.obj_:anchor(0, 0):addTo(self)endelseimage_ = self.imageParams_if self.textureResType_ == 0 thenself.obj_ = display.newSprite(string.format("#%s", image_))elseself.obj_ = display.newSprite(image_)endself.obj_:anchor(0, 0):addTo(self)endif self.setDark_ thendarkNode(self.obj_)endself:setContentSize(self.obj_:getContentSize())self.start_ = nillocal function onTouchBegan(touch, event)if not self.isEnable thenreturn falseendself.canClick = truelocal touchLocation = touch:getLocation() if self.clickRect ~= nil thenlocal parentObj = self:getParent():getParent()local n_locationInNode = parentObj:convertToNodeSpace(touchLocation)if not cc.rectContainsPoint(self.clickRect, n_locationInNode) thenreturn false endend local locationInNode = self:convertToNodeSpace(touchLocation) local s = self:getContentSize() local rect = cc.rect(0, 0, s.width, s.height) local rect2 = cc.rect(0,0,0,0) if self.label and tolua.isnull(self.label) == false then rect2.x = self.label:getPositionX() - 4 rect2.y = self.label:getPositionY() - 4 rect2.width = self.label:getPositionX() + self.label:getContentSize().width - 8 rect2.height = self.label:getPositionY() + self.label:getContentSize().height + 8 end if not self.isTouchEnabled then return end if cc.rectContainsPoint(rect, locationInNode) or cc.rectContainsPoint(rect2, locationInNode) then if self.start_ == nil thenself.start_ = touchLocationendif self.isScale_ thenself:setScale(.95*self:getScale())endreturn true end return false end local function onTouchMoved(touch, event) local touchLocation = touch:getLocation() if self.start_ ~= nil then if dir_ == "vertical" then if math.abs(touchLocation.y - self.start_.y) > 10 then self.canClick = false end elseif dir_ == "horizontal" then if math.abs(touchLocation.x - self.start_.x) > 10 then self.canClick = false end else if cc.pGetDistance(touchLocation, self.start_) > 10 then self.canClick = false end end end end local function onTouchEnded(touch, event) if self.canClick then if self.isCheckBoxType then if self.isClicked == false then self:setSelected() else self:setDefault() end else self:setSelected() end end if self.isScale_ thenself:setScale(1.05*self:getScale())end self.start_ = nil end local listener = cc.EventListenerTouchOneByOne:create() --listener:setSwallowTouches(true) listener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN) listener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED) listener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED) local eventDispatcher = self:getEventDispatcher() eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)endfunction CheckBoxEx:setClickRect(rect_)self.clickRect = rect_endfunction CheckBoxEx:setCheckBoxType()self.isCheckBoxType = truereturn selfendfunction CheckBoxEx:setSelected(call)if call == nil thencall = trueendif not self.isEnable thenreturn endif self.isClicked == true thenif self.func_ ~= nil and call and self.repeatResponse thenself.func_(self.isClicked)endreturnendself.isClicked = truelocal image_ = nilif type(self.imageSelParams_) == "table" thenif self.imageSelParams_.isAnimate ~= nil and self.imageSelParams_.isAnimate thenlocal animateName = self.imageSelParams_.animateNameif animateName ~= "" then--是否需要加载资源ccs.ArmatureDataManager:getInstance():addArmatureFileInfo(string.format("animate/%s/%s.ExportJson", animateName, animateName))local armature_ = ccs.Armature:create(animateName):setTag(1111) :anchor(0.5, 0.5) :pos(self.obj_:getContentSize().width/2, self.obj_:getContentSize().height/2+2) :addTo(self.obj_) armature_:getAnimation():play(1)endelseif self.imageSelParams_.border == true thenlocal node_ = self.obj_:getChildByTag(1000)if node_ ~= nil and tolua.isnull(node_) == false thennode_:removeFromParent()endif self.imageSelParams_.rect thenlocal sp9 = nilif self.textureResType_ == 0 thensp9 = ccui.Scale9Sprite:createWithSpriteFrameName(self.imageSelParams_.image, self.imageSelParams_.rect)elsesp9 = ccui.Scale9Sprite:create(self.imageSelParams_.rect, self.imageSelParams_.image)end sp9:anchor(0.5, 0.5) :setContentSize(self.imageSelParams_.size) :addTo(self.obj_) :pos(self.obj_:getContentSize().width/2, self.obj_:getContentSize().height/2) :setTag(1000)elselocal sp = nilif self.textureResType_ == 0 thensp = display.newSprite("#"..self.imageSelParams_.image)elsesp = display.newSprite(self.imageSelParams_.image)endsp:anchor(0.5, 0.5) :addTo(self.obj_) :pos(self.obj_:getContentSize().width/2, self.obj_:getContentSize().height/2) :setTag(1000)endelseimage_ = self.imageSelParams_.imagelocal rect_ = self.imageSelParams_.rectlocal size_ = self.imageSelParams_.sizeif self.textureResType_ == 0 thenself.obj_:setSpriteFrame(display.newSpriteFrame(image_), rect_)elseself.obj_:initWithFile(image_)endself.obj_:setContentSize(size_)self.obj_:anchor(0, 0)endendelseimage_ = self.imageSelParams_if image_ ~= nil thenlocal color_ = self.obj_:getColor()if self.textureResType_ == 0 thenself.obj_:initWithSpriteFrame(display.newSpriteFrame(string.format("#%s", image_)))elseself.obj_:initWithFile(image_)endself.obj_:setColor(color_)self.obj_:anchor(0, 0)endendself:changeLabelColor()if self.setDark_ thendarkNode(self.obj_)endif self.func_ ~= nil and call thenself.func_(self.isClicked,self)endend--[[选中状态是否重复相应]]function CheckBoxEx:closeRepeatResponse()self.repeatResponse = falsereturn selfendfunction CheckBoxEx:setDefault(call)if not self.isEnable thenreturn endself.isClicked = falselocal image_ = nilif self.imageSelParams_.isAnimate ~= nil and self.imageSelParams_.isAnimate thenif self.obj_ ~= nil and tolua.isnull(self.obj_) == false thenif self.obj_:getChildByTag(1111) thenself.obj_:removeChildByTag(1111, true)endreturnendendif type(self.imageParams_) == "table" thenif self.imageSelParams_.border == true thenif self.obj_ ~= nil and tolua.isnull(self.obj_) == false thenlocal icon_ = self.obj_:getChildByTag(1000)if icon_ ~= nil and tolua.isnull(icon_) == false thenicon_:removeFromParent()endendelseimage_ = self.imageParams_.imagelocal rect_ = self.imageParams_.rectlocal size_ = self.imageParams_.sizeif self.textureResType_ == 0 thenself.obj_:setSpriteFrame(display.newSpriteFrame(image_), rect_)elseself.obj_:initWithFile(image_)endself.obj_:setContentSize(size_)self.obj_:anchor(0, 0)endelseimage_ = self.imageParams_local color_ = self.obj_:getColor()if self.textureResType_ == 0 thenself.obj_:initWithSpriteFrame(display.newSpriteFrame(string.format("%s", image_)))elseself.obj_:initWithFile(image_)endself.obj_:anchor(0, 0)self.obj_:setColor(color_)endif self.isCheckBoxType thenif call == nil thencall = trueendif self.func_ ~= nil and call thenself.func_(self.isClicked,self)endendself:changeLabelColor()endfunction CheckBoxEx:changeEnabled(params)self.imageParams_ = paramslocal image_ = nilif type(self.imageParams_) == "table" thenif self.imageSelParams_.border == true thenimage_ = self.imageParams_.imagelocal rect_ = self.imageParams_.rectlocal size_ = self.imageParams_.sizeself.obj_:setSpriteFrame(display.newSpriteFrame(image_), rect_)self.obj_:setContentSize(size_)self.obj_:anchor(0, 0)local node_ = self.obj_:getChildByTag(1000)if node_ ~= nil and tolua.isnull(node_) == false thennode_:removeFromParent()endccui.Scale9Sprite:createWithSpriteFrameName(self.imageSelParams_.image, self.imageSelParams_.rect) :anchor(0.5, 0.5) :setContentSize(self.imageSelParams_.size) :addTo(self.obj_) :pos(self.obj_:getContentSize().width/2, self.obj_:getContentSize().height/2) :setTag(1000)elseimage_ = self.imageParams_.imagelocal rect_ = self.imageParams_.rectlocal size_ = self.imageParams_.sizeself.obj_:setSpriteFrame(display.newSpriteFrame(image_), rect_)self.obj_:setContentSize(size_)self.obj_:anchor(0, 0)endelseimage_ = self.imageParams_if self.textureResType_ == 0 thenself.obj_:initWithSpriteFrame(display.newSpriteFrame(string.format("#%s", image_)))elseself.obj_:initWithFile(image_)endself.obj_:anchor(0, 0)endendfunction CheckBoxEx:setEnabledImage(img)self.enabled_img = display.newSprite(img)self.enabled_img:anchor(0, 0)self.enabled_img:setScale(self.obj_:getScale())self.enabled_img:setVisible(false)self.enabled_img:addTo(self,2)endfunction CheckBoxEx:setEnabled(isEnable)self.isEnable = isEnableif not isEnable thenif self.enabled_img and tolua.isnull(self.enabled_img) == false thenself.enabled_img:setVisible(true)endif self.label and tolua.isnull(self.label) == false thenself.label:setColor(cc.c3b(176, 176, 176))endelseif self.enabled_img and tolua.isnull(self.enabled_img) == false thenself.enabled_img:setVisible(false)endself:changeLabelColor()endreturn selfend--[[设置选中/取消选中]]function CheckBoxEx:changeSelected(bool, call)if self.isClicked == bool thenreturnelseif bool thenself:setSelected(call)elseself:setDefault(call)endendendfunction CheckBoxEx:scale(num)self.obj_:setScale(num or 1)return selfendfunction CheckBoxEx:changeLabelColor()if not self.label or tolua.isnull(self.label) == true thenreturnendif self.isClicked == false thenself.label:setColor(self.ttf_def_color)elseself.label:setColor(self.ttf_select_color)endreturn selfendfunction CheckBoxEx:setTitleFontSize(size)-- if self.label and tolua.isnull(self.label) == false then-- self.label:setFontSize(size)-- endreturn selfendfunction CheckBoxEx:setTitleText(text,size,bool,color)if size thenself.ttf_size = sizeend self.label = Label.new(text, self.ttf_size) self.label:addTo(self) :setColor(self.ttf_def_color) :anchor(0, 0) :pos(self.obj_:getContentSize().width , 4) if bool then self.small_label = Label.new("", (self.ttf_size * .7)) self.small_label:addTo(self) :setColor(self.ttf_def_color) :anchor(0, 0) :pos(self.label:getPositionX() + self.label:getContentSize().width + 1 , 4) endreturn selfendfunction CheckBoxEx:setTitleFontSize(color)if MJ.isInStage(self.label) thenself.label:setColor(color)endif MJ.isInStage(self.small_label) thenself.small_label:setColor(color)endendfunction CheckBoxEx:setSmallTTF(value,bool)if value and type(value) == "string" and self.small_label and tolua.isnull(self.small_label) == false thenif not bool thenself.small_label:setString(string.format('(%s)',value))elseself.small_label:setString(string.format('%s',value))endendendfunction CheckBoxEx:setTouchEnabled(value)self.isTouchEnabled = valueendfunction CheckBoxEx:setIsScale(isScale_)self.isScale_ = isScale_return selfendfunction CheckBoxEx:setColor(color)if self.obj_ and tolua.isnull(self.obj_) == false thenself.obj_:setColor(color)endreturn selfendfunction CheckBoxEx:TintTo(actionTime, b, g, r)if self.obj_ and tolua.isnull(self.obj_) == false thenself.obj_:runAction(cc.TintTo:create(actionTime, b, g, r))endendreturn CheckBoxEx

如使用如下:

--标签页代码
    self.petHeadObjList={}
    local func = function (id, isSound) 
        if isSound == nil then
            YahtzeeSound.buttonClick()
        end
        for id_, btn in pairs(self.petHeadObjList) do
            if checkint(id_) == checkint(id) then
                btn:setSelected(false)
                btn:setLocalZOrder(2)
                local btn_icon = btn:getChildren()
                local url= YahtzeeShopLayer.shoplist[id_]["url_icon_press"]
                btn_icon[2]:initWithSpriteFrame(display.newSpriteFrame(url))
                btn_icon[3]:setColor(cc.c3b(255,255,255))
                self:showShopItemsView(id_)
            else
                btn:setDefault()
                btn:setLocalZOrder(1)
                local btn_icon = btn:getChildren()
                
                local url= YahtzeeShopLayer.shoplist[id_]["url_icon"]
                btn_icon[2]:setSpriteFrame(display.newSpriteFrame(url))
                btn_icon[3]:setColor(cc.c3b(255,207,164))

            end
        end
    end
       
    for i=1,2 do
        local btn = require("app.utils.views.common.CheckBoxEx").new( YahtzeeShopLayer.shoplist[i]["url_def"], YahtzeeShopLayer.shoplist[i]["url_press"], 0, function ()
            func(i)
            -- transmitScrollViewInfo(i)
            self.item_id = i
            self:setTitle(i)
        end, "horizontal")
        btn:closeRepeatResponse()
        self.petHeadObjList[i] = btn
        btn:anchor(1-(i-1), 0.5)
            :addTo(self.bg_bottom_top)
            -- :pos(self.bg_bottom_top:getContentSize().width*.5-(3-i*2)*4,121.52)
            if i == 1 then
                btn:pos(self.bg_bottom_top:getContentSize().width*0.5055,121.52)
            else
                btn:pos(self.bg_bottom_top:getContentSize().width*0.5048,121.52)
            end
            

        local btn_icon = display.newSprite(YahtzeeShopLayer.shoplist[i]["url_icon"])
            :addTo(btn)
            :pos(btn:getContentSize().width*0.2,btn:getContentSize().height*0.5)

        local btn_ttf = LabelTTF.new("",30)
            :addTo(btn)
            :pos(btn:getContentSize().width*0.6,btn:getContentSize().height*0.5)
        GlobalLanguageHandle(btn_ttf,YahtzeeShopLayer.shoplist[i]["url_ttf"])

    end
   


猜你喜欢

转载自blog.csdn.net/ding_westbrook/article/details/80801733