Cocos2d-x3.2 EditBox的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "cocos2d.h"
 
//引入头文件和命名空间
#include "extensions/cocos-ext.h"
USING_NS_CC_EXT;
 
USING_NS_CC;
 
class GameScene : public cocos2d::Layer, public EditBoxDelegate
{
public:
    static cocos2d::Scene* createScene();
     
    virtual bool init();
     
    CREATE_FUNC(GameScene);
     
    void funCallback(Ref* pSender);
     
    virtual void editBoxEditingDidBegin(cocos2d::extension::EditBox* editBox);
    virtual void editBoxEditingDidEnd(cocos2d::extension::EditBox* editBox);
    virtual void editBoxTextChanged(cocos2d::extension::EditBox* editBox, const std::string &text);
    virtual void editBoxReturn(cocos2d::extension::EditBox *editBox);
    void toExtensionsMainLayer(cocos2d::Ref *sender);
     
     
private:        //注意不能用auto关键字
    Size size;
    Sprite *sprite;
    cocos2d::Label *_TTFShowEditReturn;
    cocos2d::extension::EditBox *_editName;
    cocos2d::extension::EditBox *_editPassword;
    cocos2d::extension::EditBox *_editEmail;
    bool _drag;
};




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//  Created by Jacedy on 14-8-11.
//
//
 
#include "GameScene.h"
 
USING_NS_CC;
 
cocos2d::Scene* GameScene::createScene()
{
    auto scene = Scene::create();   //创建一个场景
    auto layer = GameScene::create();   //创建一个图层
    scene->addChild(layer);
    return scene;
}
 
//初始化当前的图层
bool GameScene::init()
{
    if(!Layer::init())      //初始化父类
        return false;
     
    //获取屏幕大小
    size = Director::getInstance()->getVisibleSize();
    //auto size = Director::getInstance()->getWinSize();
     
    //添加一个图片精灵
    sprite = Sprite::create("OnePiece_1.png");
    sprite->setPosition(Vec2(size.width/2, size.height/2));
    this->addChild(sprite);
     
    auto glview = Director::getInstance()->getOpenGLView();
    auto visibleOrigin = glview->getVisibleOrigin();
    CCLOG("%f", visibleOrigin.x);
     
    _TTFShowEditReturn = Label::createWithSystemFont("No edit Control return!""Mark Felt", 30);
    _TTFShowEditReturn->setPosition(Point(visibleOrigin.x + size.width /2, visibleOrigin.y + size.height - 50));
    addChild(_TTFShowEditReturn);
     
    //Back Menu
    auto itemBack = MenuItemFont::create("Back", CC_CALLBACK_1(GameScene::toExtensionsMainLayer, this));
    itemBack->setPosition(Point(visibleOrigin.x + size.width - 50, visibleOrigin.y + 25));
    auto menuBack = Menu::create(itemBack, NULL);
    menuBack->setPosition(Point::ZERO);
    addChild(menuBack);
     
    auto editBoxSize = Size(size.width - 100, 60);
     
    _editName = EditBox::create(editBoxSize, Scale9Sprite::create("green_edit.png"));
    _editName->setPosition(Point(visibleOrigin.x + size.width /2, visibleOrigin.y + size.height * 3 / 4));
    _editName->setFontName("Paint Boy");
    _editName->setFontSize(25);
    _editName->setFontColor(Color3B::RED);
    _editName->setPlaceHolder("Name:");
    _editName->setPlaceholderFontColor(Color3B::WHITE);
    _editName->setMaxLength(8);
    _editName->setReturnType(EditBox::KeyboardReturnType::DONE);
    _editName->setDelegate(this);
    addChild(_editName);
     
    _editPassword = EditBox::create(editBoxSize, Scale9Sprite::create("orange_edit.png"));
    _editPassword->setPosition(Point(visibleOrigin.x + size.width / 2, visibleOrigin.y + size.height / 2));
    _editPassword->setFont("American Typewriter", 30);
    _editPassword->setFontColor(Color3B::GREEN);
    _editPassword->setPlaceHolder("Password:");
    _editPassword->setMaxLength(6);
    _editPassword->setInputFlag(EditBox::InputFlag::PASSWORD);
    _editPassword->setInputMode(EditBox::InputMode::SINGLE_LINE);
    _editPassword->setDelegate(this);
    addChild(_editPassword);
     
    //bottom
    _editEmail = EditBox::create(Size(editBoxSize.width, editBoxSize.height), Scale9Sprite::create("yellow_edit.png"));
    _editEmail->setPosition(Point(visibleOrigin.x + size.width / 2, visibleOrigin.y + size.height / 4));
    _editEmail->setAnchorPoint(Point(0.5f, 1.0f));
    _editEmail->setPlaceHolder("Email:");
    _editEmail->setInputMode(EditBox::InputMode::EMAIL_ADDRESS);
    _editEmail->setDelegate(this);
    addChild(_editEmail);
     
    return true;
}
 
void GameScene::editBoxEditingDidBegin(cocos2d::extension::EditBox* editBox)
{
    log("editBox %p DidBegin.", editBox);
}
 
void GameScene::editBoxEditingDidEnd(cocos2d::extension::EditBox* editBox)
{
    log("editBox %p DidEnd.", editBox);
}
 
void GameScene::editBoxTextChanged(cocos2d::extension::EditBox* editBox, const std::string &text)
{
    log("editBox %p TextChanged, text: %s", editBox, text.c_str());
}
 
void GameScene::editBoxReturn(cocos2d::extension::EditBox *editBox)
{
    log("editBox %p was returned.", editBox);
     
    if(_editName == editBox)
    {
        _TTFShowEditReturn->setString("Name EditBox return.");
    }
    else if (_editEmail == editBox)
    {
        _TTFShowEditReturn->setString("Email EditBox return.");
    }
}
 
void GameScene::toExtensionsMainLayer(cocos2d::Ref *sender)
{
     
}
 
void GameScene::funCallback(Ref* pSender)
{
     
}

上一篇:Cocos2d-x下使用iconv库


下一篇:lnmp0.9+wordpress网站部分报502错误