Cocos2d-x3.3Final(6)Slider常用成员函数(C++)

时间:2022-02-07 23:37:03

滑动栏:

Slider * slider = Slider::create();

slider->loadBarTexture("sliderTrack.png");//设置滑动栏的背景图片
slider->loadSlidBallTextures("sliderThumb.png", "sliderThumb.png", ""); //滑动栏的滑动按钮图片
slider->loadProgressBarTexture("sliderProgress.png"); //滑动栏的进度图片
slider->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y + 100));
slider->addEventListener(CC_CALLBACK_2(HelloWorld::sliderEvent, this)); //设置事件监听函数

this->addChild(slider);


void HelloWorld::sliderEvent(Ref * pSender, Slider::EventType type)
{
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
{
Slider * slider = dynamic_cast<Slider *>(pSender);
int percent = slider->getPercent();//获取当前滑动的百分比
auto label = static_cast<Label *>(getChildByTag(11));
label->setString(String::createWithFormat("Perfecnt %d", percent)->getCString());//使用该百分比
}
}