How can i get the text from TextField into a string variable? I declared textfield and variable in .h file like so:
NewScene.h
cocos2d::ui::TextField* textField;
std::string enteredData;
then declared the textField
in .cpp init() function, and after the menu button is pressed i want to save what was written in there into variable, so i use
enteredData=textField->getString();
but the program crashes, giving me an access violation error. Could anyone tell me how to resolve this issue?
edit
here's the code:
bool NewScene::init()
...
auto textField = ui::TextField::create("Nick: ", "fonts/Marker Felt.ttf", 30);
textField->setTextHorizontalAlignment(TextHAlignment::CENTER);
textField->setTextVerticalAlignment(TextVAlignment::CENTER);
textField->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
textField->setTouchAreaEnabled(true);
textField->setTouchSize(Size(200, 100));
textField->addEventListener(CC_CALLBACK_2(NewScene::textFieldEvent, this));
this->addChild(textField);
and the function:
void NewScene::textFieldEvent(Ref *pSender, cocos2d::ui::TextField::EventType type)
{
switch (type)
{
case ui::TextField::EventType::ATTACH_WITH_IME:
{
CCLOG("Clicked");
break;
}
case ui::TextField::EventType::DETACH_WITH_IME:
{
enteredData = textField->getString();
break;
}
}
I though it would save the text to the variable after the typing is finished, but i guess it doesnt work that way.