//包含头文件
#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
//搞定,哈哈。
配图:
Cocos2d-x中拖动滑块的控件类CCControlSlider使用
最新推荐文章于 2019-04-23 18:11:24 发布