Cocos2dx (C++) – EditBox – How to retrieve multiple text data from EditBox

Posted on Updated on

It’s been a while since I posted any coding stuff here. But today after hours of debugging I have finally figured out how to solve my problem.

Objective; Using the editBoxReturn method, retrieve seperate from multiple parameters for user login.

In my .h header file I first have two private string variables as placeholders


class HogeHoge: public cocos2d::Layer, public cocos2d::ui::EditBoxDelegate
{

 private:
    std::string user_id_str;
    std::string password_str;
// etc etc
{;

In my .cpp implementation:

// etc etc
// username
 auto userID= cocos2d::ui::EditBox::create(Size(642, 146), "userid_background.png");
 userID->setPlaceHolder("Enter username");
 userID->setPosition(Point(winSize.width/2, winSize.height/2+300));
 userID->setFontSize(40);
 userID->setDelegate(this);
 userID->setReturnType(cocos2d::ui::EditBox::KeyboardReturnType::DONE);
 addChild(userID);

//password
 auto passwordText= cocos2d::ui::EditBox::create(Size(642, 146),"password_background.png");
 passwordText->setPlaceHolder("Enter password");
 passwordText->setPosition(Point(winSize.width/2, winSize.height/2+100));
 passwordText->setFontSize(40);
 passwordText->setDelegate(this);
 passwordText->setInputFlag(cocos2d::ui::EditBox::InputFlag::PASSWORD);
 addChild(passwordText);
// etc etc

and further down the code …

void HogeHoge::editBoxReturn(cocos2d::ui::EditBox* editbox) {
	this->user_id_str= editbox->getText(); break;
}

The problem: There is no distinction between userID and passwordText.
The solution: using getTag() and then pass an integer value with some kind of true false statement. Our code then looks like this:

.cpp implementation:

// etc etc
// username
 auto userID= cocos2d::ui::EditBox::create(Size(642, 146), "userid_background.png");
 userID->setPlaceHolder("Enter username");
 userID->setPosition(Point(winSize.width/2, winSize.height/2+300));
 userID->setFontSize(40);
 userID->setDelegate(this);
 userID->setTag(100);  // <- this is important
 userID->setReturnType(cocos2d::ui::EditBox::KeyboardReturnType::DONE);
 addChild(userID);

//password
 auto passwordText= cocos2d::ui::EditBox::create(Size(642, 146),"password_background.png");
 passwordText->setPlaceHolder("Enter password");
 passwordText->setPosition(Point(winSize.width/2, winSize.height/2+100));
 passwordText->setFontSize(40);
 passwordText->setDelegate(this);
 passwordText->setTag(200);  // <- this is important
 passwordText->setInputFlag(cocos2d::ui::EditBox::InputFlag::PASSWORD);
 addChild(passwordText);
// etc etc

and further down the code …

void HogeHoge::editBoxReturn(cocos2d::ui::EditBox* editbox) {
	switch(editbox->getTag()) {  // <- see what we did here?
		case 100: this->user_id_str= editbox->getText(); break;
		case 200: this->password_str = editbox->getText(); break;
	}
}

To test if this works, just use CCLog to print out the values of user_id_str or print them out as labels in your app. I tested mine on my phone rather than the emulator …because of a lot of problems and windows hates me. So I print out the values in the app like this:

auto copiedString = Label::create(this->user_id_str.c_str(), "Arial",100);
copiedString->setPosition(Point(winSize.width/2, winSize.height/2+550));
this->addChild(copiedString);

auto copiedPasswordString = Label::create(this->password_str.c_str(), "Arial",100);
copiedPasswordString->setPosition(Point(winSize.width/2, winSize.height/2+250));
this->addChild(copiedPasswordString);

Compile and run this and there we go!

Happy coding!

References:
http://toro.2ch.sc/test/read.cgi/gamedev/1390136237/756n-
http://xb.dachuw.com/cocosapi/df/d16/classcocos2d_1_1extension_1_1_edit_box_delegate.html

Leave a comment