In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to use the slider control CCControlSlider in cocos2dx. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
[CCControlSlider]
Slider control CCControlSlider, should also be a very common UI control, you must be no stranger.
For example, adjust the volume:
1. CCControl mainly provides the following control events to the slider class CCControlSlider
/ / CCControlEventValueChanged / / triggers when the value of the control changes. / /
2. The method of binding control events
/ bind control event / / addTargetWithActionForControlEvents (this, cccontrol_selector (HelloWorld::valueChanged), CCControlEventValueChanged); void addTargetWithActionForControlEvents (CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvents); / / delete control event / / removeTargetWithActionForControlEvents (this, cccontrol_selector (HelloWorld::valueChanged), CCControlEventValueChanged); void removeTargetWithActionForControlEvents (CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvents); / /
3. Files and namespaces that need to be referenced
/ / # include "cocos-ext.h" / / contains the cocos-ext.h header file using namespace cocos2d::extension; / / references cocos2d::extension namespace / /
4. Common operations
/ / class CCControlSlider: public CCControl {/ * two ways to create CCControlSlider * / create slider controls / / create with CCSprite wizard ("slider background map", "cross area map", "slider map"); static CCControlSlider* create (CCSprite* backgroundSprite, CCSprite* pogressSprite, CCSprite* thumbSprite) / / create it directly with the image resource name, omitting the step of creating a wizard / / in fact, it still calls the above create function to create a CCSprite wizard / / create ("slider background map", "cross area map", "slider map"); static CCControlSlider* create (const char* bgFile, const char* progressFile, const char* thumbFile) / * attribute settings * setValue, * setMinimumValue, setMaximumValue * setMinimumAllowedValue, setMaximumAllowedValue, * setBackgroundSprite, setProgressSprite, setThumbSprite * / float setValue () | getValue (); / / set the current slider value float setMinimumValue () | getMinimumValue (); / / set the slider minimum value float setMaximumValue () | getMaximumValue () / / set the maximum value of the slider float setMinimumAllowedValue () | setMinimumAllowedValue (); / / set the minimum value allowed to slide float setMaximumAllowedValue () | setMaximumAllowedValue (); / / set the maximum value of the slider / / set the background map of the slider, cross the area map, and the slider map / / CC_SYNTHESIZE_RETAIN is a macro definition: in addition to creating a set/get, retain is also used to retain memory resources. CC_SYNTHESIZE_RETAIN (CCSprite*, m_backgroundSprite, BackgroundSprite); CC_SYNTHESIZE_RETAIN (CCSprite*, m_progressSprite, ProgressSprite); CC_SYNTHESIZE_RETAIN (CCSprite*, m_thumbSprite, ThumbSprite);}; / /
5. analysis of the value, maximum value and allowable maximum value of the slider
Suppose the slider's property values are set as follows:
Value = 40
MinimumValue = 0; MaximumValue = 100
MinimumAllowedValue = 20; MaximumAllowedValue = 80
Then the slider can only slide between the minimum and maximum values of Allowed, that is, between the values of [20pj80].
[code actual combat]
1. Resource picture
Why does the uploaded PNG image seem to change when it appears on the blog post? What about the transparent background?
2. Introduce files and namespaces
/ / # include "cocos-ext.h" / / contains the cocos-ext.h header file using namespace cocos2d::extension; / / references cocos2d::extension namespace / /
3. Declare the callback function of the control in HelloWorld.h and the Label that displays the slider value.
/ / CCLabelTTF* label; / / Label void valueChanged (CCObject* sender, CCControlEvent controlEvent) showing the value of the slider; / / Control event triggered when the value changes / /
4. Create a slider control in HelloWorld.cpp and bind the ValueChanged control event
/ create a label label = CCLabelTTF::create ("50.0", "Arial", 32) that displays the Slider value; label- > setPosition (midPos + ccp (0100)); this- > addChild (label); / / create a slider control CCControlSlider CCControlSlider* slider = CCControlSlider::create ("sliderTrack.png", "sliderProgress.png", "sliderThumb.png"); slider- > setPosition (midPos); this- > addChild (slider) / / set the properties slider- > setMinimumValue (0); / / set the slider minimum value slider- > setMaximumValue; / / set the slider maximum value slider- > setMinimumAllowedValue (20); / / set the minimum value allowed to slide slider- > setMaximumAllowedValue (80); / / set the maximum value allowed to slide slider- > setValue (50) / / sets the value of the current slider / / binds the event. When the value of the slider changes, the control event slider- > addTargetWithActionForControlEvents (this, cccontrol_selector (HelloWorld::valueChanged), CCControlEventValueChanged) is triggered; / /
5. Implement the callback function of the control
/ / event response function. When the value changes, modify the content of the label tag void HelloWorld::valueChanged (CCObject* sender, CCControlEvent controlEvent) {/ / get the event delegate: slider control CCControlSlider CCControlSlider* slider = (CCControlSlider*) sender / / modify the value of label to show the current value of the slider / / sprintf to string format conversion in C language, where float is converted to string char str [50]; sprintf (str, "% f", slider- > getValue ()); / / string format conversion label- > setString (str);} / /
6. Running result
Thank you for reading! This is the end of the article on "how to use the slider control CCControlSlider in cocos2dx". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.