Cocos2d-x中拖动滑块的控件类CCControlSlider使用

//包含头文件
#include"cocos-ext.h"
USING_NS_CC_EXT;  //使用命名空间extension

//在init函数中加入
//_pSliderLabel为CCLabelTTF类型,用来显示滑块滑动时候的数字

_pSliderLabel = CCLabelTTF::create("", "", 30);
_pSliderLabel->setPosition(ccp(200,200));
addChild(_pSliderLabel);

 
//创建CCControlSlider空间,参数为背景图片,进度条图片,滑块图片
CCControlSlider *pSlider = CCControlSlider::create("sliderTrack.png", "sliderProgress.png", "sliderThumb.png");

//设置最大最小值
pSlider->setMinimumValue(0.0f);
pSlider->setMaximumValue(10.0f);

pSlider->setPosition(ccp(size.width/3, size.height/3));

 

// 添加回调函数,当滑块被拖动时被调用
pSlider->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::changeValue), CCControlEventValueChanged);

/*参数三:Kinds of possible events for the control objects.

 CCControlEventTouchDown           = 1 << 0,    // A touch-down event in the control.

 CCControlEventTouchDragInside     = 1 << 1,    // An event where a finger is draggedinside the bounds of the control.

 CCControlEventTouchDragOutside    = 1 << 2,    // An event where a finger is draggedjust outside the bounds of the control.

 CCControlEventTouchDragEnter      = 1 << 3,    // An event where a finger is draggedinto the bounds of the control.

 CCControlEventTouchDragExit       = 1 << 4,    // An event where a finger is draggedfrom within a control to outside its bounds.

 CCControlEventTouchUpInside       = 1 << 5,    // A touch-up event in the controlwhere the finger is inside the bounds of the control.

 CCControlEventTouchUpOutside      = 1 << 6,    // A touch-up event in the controlwhere the finger is outside the bounds of the control.

 CCControlEventTouchCancel         = 1 << 7,    // A system event canceling thecurrent touches for the control.

 CCControlEventValueChanged        = 1 << 8      // A touch dragging or otherwisemanipulating a control, causing it to emit a series of different values.
*/
addChild(pSlider);


//添加回调函数
void HelloWorld::changeValue(CCObject *sender, CCControlEvent controlEvent)
{
    CCControlSlider *pSlider = (CCControlSlider *)sender;
    //通过获取值来设置label的值
    _pSliderLabel->setString(CCString::createWithFormat("value = %f", pSlider->getValue())->getCString());
}

//编译连接,发现链接出错,无法解析的外部符号,汗啊。

//百度一下吧:原来是没有找到连接的文件

//解决办法:

//右键项目属性->配置属性->链接器里面的附加依赖项,加上libCocosDenshion.lib和libExtensions.lib

//搞定,哈哈。
配图:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值