直接上代码
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;
//滑杆回调
void slideCallback(Object *sender,Control::EventType controlEvent);
.cpp文件
//ControlSlider
//创建controlSlide,第一个参数为拖动前的图片,第二个参数为拖动后的图片,第三个参数为拖动杆
auto slide_control = ControlSlider::create("hpbg.png","hp.png","hpControl.png");
slide_control->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
slide_control->setMinimumValue(0.0f);//设置最小值
slide_control->setMaximumValue(100.0f);//设置最大值
slide_control->setValue(0.0f);//设置初始值
slide_control->setTag(20);
slide_control->addTargetWithActionForControlEvents(this,cccontrol_selector(MenuControl::slideCallback),Control::EventType::VALUE_CHANGED);//设置拖动回调
this->addChild(slide_control,2);
void MenuControl::slideCallback(Object *sender,Control::EventType controlEvent) { auto slide_control = (ControlSlider*)sender;//通过回调参数sender 获得ControlSlider int current_value = slide_control->getValue();//获取slide当前的值 }
原文链接:https://www.f2er.com/cocos2dx/345372.html