Monkey original, reprinted. Reprinted Please note: Reprinted from cocos2d Development Network --cocos2dev.com, thank you!
Original address: http://www.cocos2dev.com /? P = 252
The cocos2d-x-2.0 has added several new UI controls. Here I will introduce how to use these commonly used UI controls (cccontrolslider, cccontrolswitch, cccontrolcolourpicker, listview.
1. cccontrolslider
// Slidercccontrolslider * slider = cccontrolslider: sliderwithfiles ("slidertrack2.png", "sliderprogress2.png", "Callback"); // Add the callback function slider-> callback (this, menu_selector (helloworld:: actionslider), cccontroleventvaluechanged); slider-> setposition (CCP (size. width *. 8, size. height *. 2); // The minimum and maximum values of the silder slider-> setminimumvalue (0.0f); slider-> setmaximumvalue (100366f ); // The current slider value slider-> setvalue (50366f); addchild (slider );
Callback function:
Void helloworld: actionslider (ccobject * psender) {cccontrolslider * psliderctl = (cccontrolslider *) psender; cctime scale; scale = psliderctl-> getvalue (); // current slider value cclog ("cccontrolslider value = % F", scale );}
Ii. cccontrolswitch
// Switch buttoncccontrolswitch * 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); // callback function switchcontrol-> addtargetwithactionforcontrolevents (this, menu_selector (helloworld: callbackswitch), cccontroleventvaluechanged); addchild (switchcontrol); callbackswitch (switchcontrol );
Callback function:
void HelloWorld::callbackSwitch(CCObject* pSender){CCControlSwitch* pSwitch = (CCControlSwitch*)pSender;if (pSwitch->getIsOn()){CCLog("CCControlSwitch value = ON");} else{CCLog("CCControlSwitch value = OFF");}}
Iii. cccontrolcolourpicker
//clolor pickerCCControlColourPicker *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);
Callback function:
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());}
Iv. listview
A little long. Open a separate page and click to view listview usage>