今天继续Cocos2D-x的基础CCControlPotentiometer and CCControlSlider,到目前为至我想大家已经很了解学习的基本方法了,所以直接上代码吧:
bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
CCControlPotentiometer* potentiometer = CCControlPotentiometer::create("potentiometerTrack.png","potentiometerProgress.png","potentiometerButton.png");
potentiometer->setPosition(ccp(100, 200));
potentiometer->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::onValueChange),CCControlEventValueChanged);
this->addChild(potentiometer);
CCControlSlider * slider = CCControlSlider::create("background.png","progress.png","sliderThumb.png");
slider->setPosition(100,80);
slider->setMinimumValue(0);
slider->setMaximumValue(10000);
slider->setValue(4000);
this->addChild(slider);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
void HelloWorld::onValueChange( CCObject* pSender, CCControlEvent event )
{
CCControlPotentiometer* potentiometer = (CCControlPotentiometer* )pSender;
CCString* valueStr = CCString::createWithFormat("%f", potentiometer->getValue());
CCLOG(valueStr->getCString());
}
效果如下: