0

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.

  • Hi, post your code to identify the source of error. enteredData=textField->getString() is the correct usage, so problem is elsewhere. – Elensar May 19 '15 at 07:43
  • @Elensar Added the code. Do you know what can cause this kind of problem? – Dominik Oborowski May 19 '15 at 10:25
  • 1
    Delete auto keyword, you already have variable in the header file. With current code "auto textField" textField is never assigned for later use "textField->getString()". – Elensar May 19 '15 at 10:31

0 Answers0