[Cocos2d-x v3.x]EditBox和ControlSlider控件应用

转载请注明来自:star特530的CSDN博客  http://blog.csdn.net/start530/article/details/18993085 

示例主要是来自于 start特530的CSDN博客:我使用的cocos2d-x 3.0正式版并在原有的示例做一点小修改。

3.0正式版 将Object 修改为 ref 

这次就写个输入框和拖动条的结合使用吧。

也就是在EditBox中输入数值后,ControlSlider的状态会发生改变;拖动ControlSlider后,EditBox的数值也会发生改变。

过程如下:

1、 添加EditBoxControlSlider到场景中;

2、 当EditBox的数值发生改变时,改变ControlSlider的状态;

3、 当ControlSlider的状态发生改变时,改变EditBox的值。

代码:

头文件主要代码:

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "cocos-ext.h"

USING_NS_CC_EXT;

class HelloWorld : public cocos2d::Layer, public cocos2d::extension::EditBoxDelegate
{
public:
    // there's no 'id' in cpp, so we recommend returning the class instance pointer
    static cocos2d::Scene* createScene();

    // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    virtual bool init();  
    
    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);
    
    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
    
    //输入伪托
    // This method is called when an edit box gains focus after keyboard is shown.
    void editBoxEditingDidBegin(EditBox* editBox);
    
    // This method is called when an edit box loses focus after keyboard is hidden.
    void editBoxEditingDidEnd(EditBox* editBox);
    
    // This method is called when the edit box text was changed.
    void editBoxTextChanged(EditBox* editBox, const std::string& text);
    
    // This method is called when the return button was pressed or the outside area of keyboard was touched.
    void editBoxReturn(EditBox* editBox);
    
    //滑杆回调
    void slideCallback(cocos2d::Ref* sender, Control::EventType controlEvent);
};

#endif // __HELLOWORLD_SCENE_H__

cpp实现文件:

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    
    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 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);
    
    //EditBox
    auto inputBox = Sprite::create("inputBox.png");//添加输入框的背景框
    inputBox->setPosition(Point(visibleSize.width/2,visibleSize.height/3));
    this->addChild(inputBox,1);
    
    EditBox* editBox = EditBox::create(Size(100, 35.0), Scale9Sprite::create());//创建EditBox,第一个参数设置输入框的大小,第二个参数和和“九妹”相关
    editBox->setPosition(Point(visibleSize.width/2,visibleSize.height/3));
    //editBox->setPlaceHolder("请输入数字");//设置editBox输入为空时的显示状态
    editBox->setInputMode(EditBox::InputMode::NUMERIC);//输入模式,这里设置为数字
    editBox->setDelegate(this);//开启委托
    editBox->setFontColor(Color3B::BLACK);//设置文字颜色
    editBox->setText("0");//设置默认显示数字
    editBox->setTag(10);
    this->addChild(editBox,2);
    
    //ControlSlider
    //创建controlSlide,第一个参数为拖动前的图片,第二个参数为拖动后的图片,第三个参数为拖动杆
    auto slide_control = ControlSlider::create("loadingbar.png","sliderProgress.png","sliderTrack.png");
    slide_control->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
    slide_control->setMinimumValue(0.0f);//设置最小值
    slide_control->setMaximumValue(100.0f);//设置最大值
    slide_control->setValue(0.0f);//设置初始值
    slide_control->setTag(20);
    slide_control->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::slideCallback),Control::EventType::VALUE_CHANGED);//设置拖动回调
    this->addChild(slide_control,2);
    
    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
	MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    return;
#endif

    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}

void HelloWorld::editBoxTextChanged(EditBox* editBox, const std::string& text)
{
    int num_int = std::atoi(text.c_str());//将字符串转成整型
    int maxNum = 100;//因为controlSlider 的最大值为100,所以这里输入的值最大也不能超过100
    if(num_int > maxNum)
    {
        num_int = maxNum;
    }
    
    char buf_str[16];
    sprintf(buf_str,"%d",num_int);//将int的数值放入buf_str中
    editBox->setText(buf_str);//重新设置editBox的值
    
    //改变ControlSlider的值
    auto slide = (ControlSlider*)this->getChildByTag(20);//通过tag获取controlSlider
    slide->setValue((float)num_int);//重新设置slide的值
    
}

void HelloWorld::slideCallback(Ref *sender, Control::EventType controlEvent)
{
    auto slide_control = (ControlSlider*)sender;//通过回调参数sender 获得ControlSlider
    int current_value = slide_control->getValue();//获取slide当前的值
    
    char buf_str[16];
    sprintf(buf_str,"%d",current_value);
    
    auto editBox = (EditBox*)this->getChildByTag(10);//通过tag获得EditBox
    editBox->setText(buf_str);//设置editBox的值
}


void HelloWorld::editBoxReturn(cocos2d::extension::EditBox*){}
void HelloWorld::editBoxEditingDidEnd(cocos2d::extension::EditBox*){}
void HelloWorld::editBoxEditingDidBegin(cocos2d::extension::EditBox*){}


由于没有找到合适图片,只是熟悉控件的应用:

效果图如下:

[Cocos2d-x v3.x]EditBox和ControlSlider控件应用_第1张图片


你可能感兴趣的:([Cocos2d-x v3.x]EditBox和ControlSlider控件应用)