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
[Cpp]
// Slider
CCControlSlider * slider = CCControlSlider: sliderWithFiles ("sliderTrack2.png", "sliderProgress2.png", "sliderThumb.png ");
// Add a callback function
Slider-> addTargetWithActionForControlEvents (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 );
// Current value of slider
Slider-> setValue (50366f );
AddChild (slider );
Callback function:
[Cpp]
Void HelloWorld: actionSlider (CCObject * pSender ){
CCControlSlider * pSliderCtl = (CCControlSlider *) pSender;
CcTime scale;
Scale = pSliderCtl-> getValue ();
// Current value of slider
CCLog ("CCControlSlider value = % f", scale );
}
Ii. CCControlSwitch
[Cpp]
// 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 ));
// Callback function
SwitchControl-> addTargetWithActionForControlEvents (this, menu_selector (HelloWorld: callbackSwitch), CCControlEventValueChanged );
AddChild (switchControl );
CallbackSwitch (switchControl );
Callback function:
[Cpp]
Void HelloWorld: callbackSwitch (CCObject * pSender ){
CCControlSwitch * pSwitch = (CCControlSwitch *) pSender;
If (pSwitch-> getIsOn ()){
CCLog ("CCControlSwitch value = ON ");
} Else {
CCLog ("CCControlSwitch value = OFF ");
}
}
Iii. CCControlColourPicker
[Cpp]
// 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 );
Callback function:
[Cpp]
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 ());
}
Author: yanghuiliu