UI开发之控件类-CCControlSlider

bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
       
        CC_BREAK_IF(! CCLayer::init());

		//设置一个显示字符串的label
		CCLabelTTF * title = CCLabelTTF::create("slider value = 0.00","Arial",32);
		title->setPosition(ccp(240,200));
		//设置label的tag为1,方便以后获取
		this->addChild(title,1,1);

		CCControlSlider * slider = CCControlSlider::create("sliderTrack.png","sliderProgress.png",
			"sliderThumb.png");

		//设置slider的最大值最小值
		slider->setMaximumValue(100.0);
		slider->setMinimumValue(0.0);

		//为slider添加事件监听函数
		slider->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::valueChanged),
			CCControlEventValueChanged);

		//设置位置
		slider->setPosition(ccp(240,160));
		this->addChild(slider);

        bRet = true;
    } while (0);

    return bRet;
}
void HelloWorld::valueChanged(CCObject * pSender,CCControlEvent controlEvent)
 {
	CCControlSlider * slider = (CCControlSlider *)pSender;
	CCLabelTTF * title = (CCLabelTTF *)this->getChildByTag(1);
	title->setString(CCString::createWithFormat("slider value = %0.02f",slider->getValue())->getCString());
 }

UI开发之控件类-CCControlSlider_第1张图片

你可能感兴趣的:(cocos2d-x,cocos2d-x,游戏)