【Cocos2d-x】CCControlButton状态按钮的使用

本文介绍了Cocos2d-x框架中的ControlButton控件,详细讲解了其作为状态按钮的功能,包括不同触摸事件如TouchDown、TouchUpInside等,并提供了ControlSwitch和ControlSlider的简要说明。
摘要由CSDN通过智能技术生成


继承关系


api文档地址:http://cn.cocos2d-x.org/doc/cocos2d-x-3.0/da/d32/classcocos2d_1_1extension_1_1_control.html


#include <CCControl.h>

类 Control 继承关系图:


ControlSwitch:开关控件

ControlSlider:滑块控件

ControlButton:状态按钮控件



ControlSwitch


开关控件示例代码(基于Cocos2d-x2.1.5):


-- 振动
    local shakeSwitch = nil
    -- 回调
    local function shakeCallback()
        if shakeSwitch:isOn() then
            cclog("open shake")
        else
            cclog("close shake")
        end
    end
    shakeSwitch = CCControlSwitch:create(
        -- 背景图片        
        CCSprite:create("res/ui/setting/switch-mask.png"),
        -- 开启时背景        
        CCSprite:create("res/ui/setting/switch-on.png"),
        -- 关闭时背景        
        CCSprite:create("res/ui/setting/switch-off.png"),
        -- 拖动图标        
        CCSprite:create("res/ui/setting/switch-thumb.png"),
        -- 开启文字(非必要参数)        
        CCLabelTTF:create("On", "Arial-BoldMT", 16),
        -- 关闭文字(非必要参数)        
        CCLabelTTF:create("Off", "Arial-BoldMT", 16)
    )
    -- 注册监听    
    shakeSwitch:addHandleOfControlEvent(shakeCallback, CCControlEventValueChanged)
    -- 设置触摸优先级    
    shakeSwitch:setTouchPriority(-128)
    shakeSwitch:setAnchorPoint(ccp(0,0.5))
    shakeSwitch:setPosition(ccp(490,210))
    layer:addChild(shakeSwitch)


ControlSlider


可拖动控件示例代码(基于Cocos2d-x2.1.5):


    local slider = CCControlSlider:create(
    -- 背景图片
    "res/ui/setting/slider_bg.png",
    -- 进度图片
    "res/ui/setting/slider_progress.png" ,
    -- 拖动图标            
    "res/ui/setting/btn.png")

    -- 回调    
    local function musicCallback()
        -- 当前进度        
        local value = musicSlider:getValue()
        cclog("music slider value=%f",value)
        SimpleAudioEngine:sharedEngine():setBackgroundMusicVolume(value)
    end
    musicSlider:setAnchorPoint(ccp(0, 0.5))
    -- 取值范围(最大、最小值)
    musicSlider:setMinimumValue(0.0)
    musicSlider:setMaximumValue(1.0)
    
    -- 可拖动范围(允许的最大、最小值)
    musicSlider:setMaximumAllowedValue(0.2)
    musicSlider:setMinimumAllowedValue(0.8)
    
    -- 设置当前值    
    musicSlider:setValue(3.0)
    musicSlider:setPosition(ccp(visibleSize.width/2,248))
    -- 注册监听
    musicSlider:addHandleOfControlEvent(musicCallback, CCControlEventValueChanged)
    -- 设置触摸优先级    
    musicSlider:setTouchPriority(-128)
    
    layer:addChild(musicSlider)



ControlButton



可监听按钮的各种状态:

1.CCControlEventTouchDown             按钮按下

2.CCControlEventTouchDragInside     按下,在按钮内拖曳

3.CCControlEventTouchDragOutside   按下,在按钮外拖曳

4.CCControlEventTouchDragEnter       按下按钮,手指移入按钮内时触发

5.CCControlEventTouchDragExit          按下按钮,手指移出按钮外时触发

6.CCControlEventTouchUpInside         手指在按钮中抬起

7.CCControlEventTouchUpOutside      手指在按钮外抬起

8.CCControlEventTouchCancel            取消触摸



--Add the button
        local pBackgroundButton            = CCScale9Sprite:create("extensions/button.png")
        local pBackgroundHighlightedButton = CCScale9Sprite:create("extensions/buttonHighlighted.png")
        
        local pTitleButtonLabel = CCLabelTTF:create("Touch Me!", "Marker Felt", 30)
        pTitleButtonLabel:setColor(ccc3(159, 168, 176))
        
        -- 创建状态按钮
        local pControlButton = CCControlButton:create(
            -- 按钮标题(非必要参数,CCNode or string)
            pTitleButtonLabel, 
            -- 按钮背景
            pBackgroundButton)
        local function touchDownAction()
        	if nil == pDisplayValueLabel then
        		return
        	end       	
        	pDisplayValueLabel:setString(CCString:create("Touch Down"):getCString())
        	print("Touch Down")
        end
        
        local function touchDragInsideAction()
        	if nil == pDisplayValueLabel then
        		return
        	end 
        	pDisplayValueLabel:setString(CCString:create("Drag Inside"):getCString())
        	print("Touch DragInside")
        end
        
        local function touchDragOutsideAction()
        	if nil == pDisplayValueLabel then
        		return
        	end 
        	pDisplayValueLabel:setString(CCString:create("Drag Outside"):getCString())
        	print("Touch DragOutside")
        end
        
        local function touchDragEnterAction()
        	if nil == pDisplayValueLabel then
        		return
        	end 
        	pDisplayValueLabel:setString(CCString:create("Drag Enter"):getCString())
        	print("Touch DragEnter")
        end
        
        local function touchDragExitAction()
        	if nil == pDisplayValueLabel then
        		return
        	end 
        	pDisplayValueLabel:setString(CCString:create("Drag Exit"):getCString())
        	print("Touch DragExit")
        end
        
        local function touchUpInsideAction()
        	if nil == pDisplayValueLabel then
        		return
        	end 
        	pDisplayValueLabel:setString(CCString:create("Touch Up Inside."):getCString())
        	print("Touch UpInside")
        end
        
        local function touchUpOutsideAction()
        	if nil == pDisplayValueLabel then
        		return
        	end 
        	pDisplayValueLabel:setString(CCString:create("Touch Up Outside."):getCString())
        	print("Touch UpOutside")
        end
        
        local function touchCancelAction()
        	if nil == pDisplayValueLabel then
        		return
        	end 
        	pDisplayValueLabel:setString(CCString:create("Touch Cancel"):getCString())
        	print("Touch Cancel")
        end
        
        -- 根据按钮状态设置按钮背景
        pControlButton:setBackgroundSpriteForState(pBackgroundHighlightedButton, CCControlStateHighlighted)
        -- 根据按钮状态设置标题颜色
        pControlButton:setTitleColorForState(ccc3(255, 255, 255), CCControlStateHighlighted)
        
        pControlButton:setAnchorPoint(ccp(0.5, 1))
        pControlButton:setPosition(ccp(screenSize.width / 2.0, screenSize.height / 2.0))
            
        -- 注册状态监听    
        pControlButton:addHandleOfControlEvent(touchDownAction,CCControlEventTouchDown)
        pControlButton:addHandleOfControlEvent(touchDragInsideAction,CCControlEventTouchDragInside)
        pControlButton:addHandleOfControlEvent(touchDragOutsideAction,CCControlEventTouchDragOutside)
        pControlButton:addHandleOfControlEvent(touchDragEnterAction,CCControlEventTouchDragEnter)
        pControlButton:addHandleOfControlEvent(touchDragExitAction,CCControlEventTouchDragExit)
        pControlButton:addHandleOfControlEvent(touchUpInsideAction,CCControlEventTouchUpInside)
        pControlButton:addHandleOfControlEvent(touchUpOutsideAction,CCControlEventTouchUpOutside)
        pControlButton:addHandleOfControlEvent(touchCancelAction,CCControlEventTouchCancel)
        pLayer:addChild(pControlButton, 1)





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值