Cocos2dx 3.0 (7) story about EditBox and ControlSlider

Source: Internet
Author: User

When I started my work in the eighth day, I was very impressed.

Many things have taken place during the Lunar January holiday. It has a great impact on me.

I have been depressed for several days. Now, I have been resurrected in full blood. Is it so easy for me to give up? If you are reluctant to give up, you shouldn't give up! Even if there is no hope, we should try again!

The programmer's path is to overcome various difficulties and obstacles and make it impossible.

---------Brave young man ~~~ Hey !~

---------------------------------------------------------------

This time I will write an input box and use it with a drag bar.

That is, after you enter a value in the EditBox, The ControlSlider status changes. After you drag the ControlSlider, the value of the EditBox also changes.

The process is as follows:

1. Add EditBox and ControlSlider to the scene;

2. When the value of EditBox changes, the ControlSlider status is changed;

3. When the ControlSlider status changes, the EditBox value is changed.

 

Code:

Main header file code:

# Ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H __# include "cocos2d. h "# include" cocos-ext.h "USING_NS_CC; USING_NS_CC_EXT; class HelloWorld: public cocos2d: Layer, public EditBoxDelegate {public: static cocos2d: Scene * createScene (); virtual bool init (); void menuCloseCallback (Object * pSender); CREATE_FUNC (HelloWorld); // enter the pseudovoid encode (EditBox * editBox); void editBoxEditingDidEnd (EditBox * editBox ); void editBoxTextChanged (EditBox * editBox, const std: string & text); void editBoxReturn (EditBox * editBox); // callback void slideCallback (Object * sender, Control :: eventType controlEvent) ;};# endif // _ HELLOWORLD_SCENE_H __

1. Add EditBox and ControlSlider

Bool HelloWorld: init () {if (! Layer: init () {return false;} Size visibleSize = Director: getInstance ()-> getVisibleSize (); Point origin = Director: getInstance () -> getVisibleOrigin (); auto label = LabelTTF: create ("Hello World", "Arial", 24); label-> setPosition (Point (origin. x + visibleSize. width/2, origin. y + visibleSize. height-label-> getContentSize (). height); this-> addChild (label, 1); // EditBoxauto inputBox = Sprite: create ("inputBox.png "); // Add the background box inputBox-> setPosition (Point (visibleSize. width/2, visibleSize. height/3); this-> addChild (inputBox, 1); EditBox * editBox = EditBox: create (Size (100, 35.0), Scale9Sprite: create ()); // create an EditBox. The first parameter sets the size of the input box. The second parameter and editBox-> setPosition (Point (visibleSize. width/2, visibleSize. height/3); // editBox-> setPlaceHolder ("enter a number"); // sets the display status editBox-> setInputMode (editBox :: inputMode: NUMERIC); // input mode, which is set to digital editBox-> setDelegate (this); // enable delegate editBox-> setFontColor (Color3B: BLACK ); // set the text color editBox-> setText ("0"); // set the default display number editBox-> setTag (10); this-> addChild (editBox, 2 ); // ControlSlider // create controlSlide. The first parameter is the image before the drag, the second parameter is the image after the drag, and the third parameter is the auto slide_control = ControlSlider :: create ("sliderProgress.png", "sliderTrack.png", "sliderThumb.png"); slide_control-> setPosition (Point (visibleSize. width/2, visibleSize. height/2); slide_control-> setMinimumValue (0.0f); // set the minimum value slide_control-> setMaximumValue (100366f); // set the maximum value slide_control-> setValue (0.0f ); // set the initial value slide_control-> setTag (20); slide_control-> addTargetWithActionForControlEvents (this, cccontrol_selector (HelloWorld: slideCallback), Control: EventType: VALUE_CHANGED ); // set the drag callback this-> addChild (slide_control, 2); return true ;}

Effect:



2. When EditBoxThe code in the editBoxTextChanged () function is modified as follows:

Void HelloWorld: editBoxTextChanged (EditBox * editBox, const std: string & text) {int num_int = std: atoi (text. c_str (); // convert the string to an integer int maxNum = 100; // because the maximum value of controlSlider is 100, the maximum value entered here cannot exceed 100if (num_int> maxNum) {num_int = maxNum;} char buf_str [16]; sprintf (buf_str, "% d", num_int ); // put the int value in the buf_str editBox-> setText (buf_str); // reset the editBox value // change the ControlSlider value auto slide = (ControlSlider *) this-> getChildByTag (20); // get controlSliderslide through tag-> setValue (float) num_int); // reset the value of slide}

3. When the ControlSlider status is changed, modify the code of its corresponding callback function slideCallback () as follows:

Void HelloWorld: slideCallback (Object * sender, Control: EventType controlEvent) {auto slide_control = (ControlSlider *) sender; // obtain ControlSliderint current_value = slide_control-> getValue () through the callback parameter sender; // obtain the current value of slide char buf_str [16]; sprintf (buf_str, "% d ", current_value); auto editBox = (EditBox *) this-> getChildByTag (10); // get EditBoxeditBox-> setText (buf_str) through tag; // set editBox value}


Well, that's the result.





Please specify the source for forwarding:

Http://blog.csdn.net/start530? Viewmode = contents






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.