Cocos2d-x3.4 EditBox的问题

在用cocos2d-x写一个小游戏的时候想加一个界面,让用户一开始能输入自己的姓名。
然而……搜了下网上的3.4的EditBox用法也是各种报错各种波浪线..
琢磨了一下API和别人的代码啥的算是终于跑起来了(虽然那个效果和我想的不一样……就是输入名字的时候会弹出一个inputbox)..【有种 以为自己建的是高楼,最后搭完发现是茅厕的感觉。。。】


运行下看看这个界面
Cocos2d-x3.4 EditBox的问题_第1张图片

Cocos2d-x3.4 EditBox的问题_第2张图片


主要的代码

//LoginScene.h
#ifndef _LOGIN_SCENE_H_
#define _LOGIN_SCENE_H_

#include <cocos2d.h>
#include "ui\UIScale9Sprite.h"
.........
.........
USING_NS_CC;
#include "ui\UIEditBox\UIEditBox.h"
using namespace ui; 

class LoginScene : public Scene
{
public:
    EditBox* editBox;//这是为了后来的功能给特意写成public成员了
    static cocos2d::Scene* createScene(); //LoginScene
    virtual bool init(); //初始化
    CREATE_FUNC(LoginScene);  
    void WelcomeCallBack(Ref *pSender , Widget::TouchEventType type);

    //来自API的指示 不明觉厉系列  委托啥的……
    // 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){};  
};

#endif
//LoginScene.cpp
#include "LoginScene.h"
#include "ui\UIEditBox\UIEditBox.h"
#include "WelcomeScene.h"
#include <iostream>
#include <fstream>
using namespace std;
string name;
Scene* LoginScene::createScene()
{
    auto scene = Scene::create();
    auto layer = LoginScene::create();
    scene->addChild(layer);
    return scene;
}

bool LoginScene::init()  
{  
    //////////////////////////////  
    // 1. super init first  
   /* if (!Layer::init())  //不知为何这句会有波浪线……
    {  
        return false;  
    }  */

    Size visibleSize = Director::getInstance()->getVisibleSize();  
    Vec2 origin = Director::getInstance()->getVisibleOrigin();  


    auto label = LabelTTF::create("XXXXXXXXXXXXX", "Arial", 18);  
    label->setPosition(Point(origin.x + visibleSize.width/2,  
                             origin.y + visibleSize.height - label->getContentSize().height));  
    this->addChild(label, 1);  

//这是确认键 按下确认后跳转到WelcomeScene
     auto confirm_button = Button::create("button.png");
        confirm_button->setTitleText("Confirm");
        confirm_button->setTitleFontName("微软雅黑");
        confirm_button->setTitleFontSize(16);
        confirm_button->setPosition(Vec2(240,50));
        confirm_button->addTouchEventListener(CC_CALLBACK_2(LoginScene::WelcomeCallBack,this));
        addChild(confirm_button);

    //EditBox  
    auto inputBox = Sprite::create("inputbox.png");//添加输入框的背景框  
    inputBox->setPosition(Point(visibleSize.width/2,visibleSize.height/3));  
    this->addChild(inputBox,1);  

    editBox = EditBox::create(Size(100, 35.0), Scale9Sprite::create());//创建EditBox,第一个参数设置输入框的大小,第二个参数和和“九妹”相关  
    editBox->setPosition(Point(visibleSize.width/2,visibleSize.height/3));  
    editBox->setPlaceHolder("Name:");//设置editBox输入为空时的显示状态  
//    editBox->setInputMode(EditBox::InputMode::NUMERIC);//输入模式,可以设置为数字  
//    editBox->setDelegate(this);//开启委托  
    editBox->setFont("Arial",15);
    editBox->setFontColor(Color3B::BLACK);//设置文字颜色  
    editBox->setText("Name:");//设置默认显示  
//    editBox->setTag(10);  
    this->addChild(editBox,2);  
    return true;  
}  

void LoginScene::WelcomeCallBack(Ref *pSender , Widget::TouchEventType type){    CCLOG("StartGame");
    switch (type)
    {    case cocos2d::ui::Widget::TouchEventType::ENDED:  
        Director::getInstance()->replaceScene(TransitionCrossFade::create(0.5f,WelcomeScene::createScene()));
        name = editBox->getText(); //这里的name是全局的,就是为了获取输入的文字……感觉这个方法不是很好,暂且先这么写了。
        break;
    }
}

Cocos2d-x更新的也比较快,感觉蛮多3.0 3.2的东西 到3.4就弃用了之类的。
到底include哪些头文件也是比较蛋疼……还是多多摸索吧……


有问题请大家指正

你可能感兴趣的:(cocos2d-x)