cocos2d-x-2.0新增加了几个UI控件,这里我介绍下常用的这几个UI控件(CCControlSlider、CCControlSwitch、CCControlColourPicker、ListView)的使用方法。
一、CCControlSlider
- // Slider
- CCControlSlider * slider = CCControlSlider::sliderWithFiles("sliderTrack2.png","sliderProgress2.png" ,"sliderThumb.png");
- //添加回调函数
- slider->addTargetWithActionForControlEvents(this, menu_selector(HelloWorld::actionSlider),CCControlEventValueChanged);
- slider->setPosition(ccp(size.width*.8, size.height*.2) );
- //silder的最小值和最大值
- slider->setMinimumValue(0.0f);
- slider->setMaximumValue(100.0f);
- //slider的当前值
- slider->setValue(50.0f);
- addChild(slider);
回调函数:
- void HelloWorld::actionSlider(CCObject* pSender){
- CCControlSlider* pSliderCtl = (CCControlSlider*)pSender;
- ccTime scale;
- scale = pSliderCtl->getValue();
- //slider的当前值
- CCLog("CCControlSlider value = %f",scale);
- }
二、CCControlSwitch
- //switch button
- CCControlSwitch *switchControl = CCControlSwitch::switchWithMaskSprite
- (
- CCSprite::spriteWithFile("switch-mask.png"),
- CCSprite::spriteWithFile("switch-on.png"),
- CCSprite::spriteWithFile("switch-off.png"),
- CCSprite::spriteWithFile("switch-thumb.png"),
- CCLabelTTF::labelWithString("On", "Arial-BoldMT", 16),
- CCLabelTTF::labelWithString("Off", "Arial-BoldMT", 16)
- );
- switchControl->setPosition(ccp (size.width*.8, size.height*.35));
- //回调函数
- switchControl->addTargetWithActionForControlEvents(this, menu_selector(HelloWorld::callbackSwitch), CCControlEventValueChanged);
- addChild(switchControl);
- callbackSwitch(switchControl);
回调函数:
- void HelloWorld::callbackSwitch(CCObject* pSender){
- CCControlSwitch* pSwitch = (CCControlSwitch*)pSender;
- if (pSwitch->getIsOn()){
- CCLog("CCControlSwitch value = ON");
- } else{
- CCLog("CCControlSwitch value = OFF");
- }
- }
三、CCControlColourPicker
- //clolor picker
- CCControlColourPicker *colourPicker = CCControlColourPicker::colourPicker();
- colourPicker->setColor(ccc3(37, 46, 252));
- colourPicker->setPosition(ccp (size.width*.8, size.height*.7));
- colourPicker->addTargetWithActionForControlEvents(this, menu_selector(HelloWorld::colourValueChanged), CCControlEventValueChanged);
- addChild(colourPicker);
回调函数:
- void HelloWorld::colourValueChanged(CCObject *sender){
- CCControlColourPicker* pPicker = (CCControlColourPicker*)sender;
- std::string str = CCString::stringWithFormat("#%02X%02X%02X",pPicker->getColorValue().r, pPicker->getColorValue().g, pPicker->getColorValue().b)->getCString();
- CCLog("CCControlColourPicker value = %s",str.c_str());
- }
四、ListView
有点长,单独开一个页面,点击查看ListView使用>>