Cocos2d-x UI開辟之CCControlSlider控件類應用實例。本站提示廣大學習愛好者:(Cocos2d-x UI開辟之CCControlSlider控件類應用實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Cocos2d-x UI開辟之CCControlSlider控件類應用實例正文
關於控件應用時的一些設置裝備擺設,請拜見文章:UI開辟之控件類-CCControlButton。這裡只寫出源代碼,裡邊的正文對控件的用法有具體的引見。
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,0,1); //這三張圖片分離是底部,進度條,掌握按鈕 CCControlSlider * slider = CCControlSlider::create("sliderTrack.png","sliderProgress.png", "sliderThumb.png"); //設置slider的最年夜值最小值 slider->setMaximumValue(100); slider->setMinimumValue(0); //為slider添加事宜監聽函數 slider->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::valueChanged), CCControlEventValueChanged); //設置地位 slider->setPosition(ccp(240,160)); this->addChild(slider); //應用CCControlSlider完成血量條,只需將第三個掌握按鈕改成通明的便可以了,就會貌似血量條了 CCControlSlider * slider2 = CCControlSlider::create("sliderTrack.png","progress.png","sliderThumb2.png"); //經由過程以下函數使得以為的不克不及轉變它的值 slider2->setTouchEnabled(false); slider2->setMaximumValue(100); slider2->setMinimumValue(0); //可以添加事宜處置函數,在事宜處置函數中轉變它的value slider2->setValue(100); slider2->setPosition(ccp(240,100)); this->addChild(slider2); bRet = true; } while (0); return bRet; } //留意這個函數須要傳入的倆個參數類型 void HelloWorld::valueChanged(CCObject * pSender,CCControlEvent controlEvent) { CCControlSlider * slider = (CCControlSlider *)pSender; CCLabelTTF * title = (CCLabelTTF *)this->getChildByTag(1); //應用CCLabelTTF的時刻盡可能不要從新轉變它的內容,這裡是為了輕便起見 title->setString(CCString::createWithFormat("slider value = %0.02f",slider->getValue())->getCString()); }