cocos2d-x-2.0新增加了几个UI控件,这里我介绍下常用的这几个UI控件(CCControlSlider、CCControlSwitch、CCControlColourPicker)使用方法

原文地址:   http://www.cocos2dev.com/?p=252

cocos2d-x-2.0新增加了几个UI控件,这里我介绍下常用的这几个UI控件(CCControlSlider、CCControlSwitch、CCControlColourPicker、ListView)的使用方法。

cocos2d-x-2.0新增加了几个UI控件,这里我介绍下常用的这几个UI控件(CCControlSlider、CCControlSwitch、CCControlColourPicker)使用方法_第1张图片

一、CCControlSlider

[cpp]  view plain copy
  1. // Slider  
  2.   
  3. CCControlSlider * slider = CCControlSlider::sliderWithFiles("sliderTrack2.png","sliderProgress2.png" ,"sliderThumb.png");  
  4.   
  5. //添加回调函数  
  6.   
  7. slider->addTargetWithActionForControlEvents(this, menu_selector(HelloWorld::actionSlider),CCControlEventValueChanged);  
  8.   
  9. slider->setPosition(ccp(size.width*.8, size.height*.2) );  
  10.   
  11. //silder的最小值和最大值  
  12.   
  13. slider->setMinimumValue(0.0f);  
  14.   
  15. slider->setMaximumValue(100.0f);  
  16.   
  17. //slider的当前值  
  18.   
  19. slider->setValue(50.0f);  
  20.   
  21. addChild(slider);  

回调函数:

[cpp]  view plain copy
  1. void HelloWorld::actionSlider(CCObject* pSender){  
  2.   
  3. CCControlSlider* pSliderCtl = (CCControlSlider*)pSender;  
  4.   
  5. ccTime scale;  
  6.   
  7. scale = pSliderCtl->getValue();  
  8.   
  9. //slider的当前值  
  10.   
  11. CCLog("CCControlSlider value = %f",scale);  
  12.   
  13. }  

二、CCControlSwitch

[cpp]  view plain copy
  1. //switch button  
  2.   
  3. CCControlSwitch *switchControl = CCControlSwitch::switchWithMaskSprite  
  4.   
  5. (  
  6.   
  7. CCSprite::spriteWithFile("switch-mask.png"),  
  8.   
  9. CCSprite::spriteWithFile("switch-on.png"),  
  10.   
  11. CCSprite::spriteWithFile("switch-off.png"),  
  12.   
  13. CCSprite::spriteWithFile("switch-thumb.png"),  
  14.   
  15. CCLabelTTF::labelWithString("On""Arial-BoldMT", 16),  
  16.   
  17. CCLabelTTF::labelWithString("Off""Arial-BoldMT", 16)  
  18.   
  19. );  
  20.   
  21. switchControl->setPosition(ccp (size.width*.8, size.height*.35));  
  22.   
  23. //回调函数  
  24.   
  25. switchControl->addTargetWithActionForControlEvents(this, menu_selector(HelloWorld::callbackSwitch), CCControlEventValueChanged);  
  26.   
  27. addChild(switchControl);  
  28.   
  29. callbackSwitch(switchControl);  

回调函数:

[cpp]  view plain copy
  1. void HelloWorld::callbackSwitch(CCObject* pSender){  
  2.   
  3. CCControlSwitch* pSwitch = (CCControlSwitch*)pSender;  
  4.   
  5. if (pSwitch->getIsOn()){  
  6.   
  7. CCLog("CCControlSwitch value = ON");  
  8.   
  9. else{  
  10.   
  11. CCLog("CCControlSwitch value = OFF");  
  12.   
  13. }  
  14.   
  15. }  

三、CCControlColourPicker

[cpp]  view plain cop
  1. //clolor picker  
  2.   
  3. CCControlColourPicker *colourPicker = CCControlColourPicker::colourPicker();  
  4.   
  5. colourPicker->setColor(ccc3(37, 46, 252));  
  6.   
  7. colourPicker->setPosition(ccp (size.width*.8, size.height*.7));  
  8.   
  9. colourPicker->addTargetWithActionForControlEvents(this, menu_selector(HelloWorld::colourValueChanged), CCControlEventValueChanged);  
  10.   
  11. addChild(colourPicker);  

回调函数:

[cpp]  view plain copy
  1. void HelloWorld::colourValueChanged(CCObject *sender){  
  2.   
  3. CCControlColourPicker* pPicker = (CCControlColourPicker*)sender;  
  4.   
  5. std::string str = CCString::stringWithFormat("#%02X%02X%02X",pPicker->getColorValue().r, pPicker->getColorValue().g, pPicker->getColorValue().b)->getCString();  
  6.   
  7. CCLog("CCControlColourPicker value = %s",str.c_str());  
  8.   
  9. }  

四、ListView

有点长,单独开一个页面,点击查看ListView使用>>

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