Several new UI controls are added to the cocos2d-x-2.0

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.