Search code examples
textinputfieldcocos2d-x

Cocos2dx 3.4 - manipulating user input


I am working on a breakout-type game using Cocos2dx.

I need to make a highscore table. After the game is finished, I'd like to display a page, where player types his username into text field.

I don't know how to add the user input into variable, so I can manipulate it later (mainly saving along with score to display it on the selected scene).

What is the simplest way of doing so?


Solution

  • Approach One: If you just need to handle keyboard as key-event, It's as easy as these below lines of code:

    HelloWorld::init()
    { 
        ...
        auto keyboardListener = EventListenerKeyboard::create();
        keyboardListener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event)
        {
            switch (keyCode)
            {
            case EventKeyboard::KeyCode::KEY_UP_ARROW:      /*Jump maybe*/  break;
            case EventKeyboard::KeyCode::KEY_DOWN_ARROW:    /*Crouch maybe*/    break;
            case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:   /*Move Right maybe*/    break;
            case EventKeyboard::KeyCode::KEY_LEFT_ARROW:    /*Move Left maybe*/ break;
            }
        };
        _eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this);
        ...
    return true;
    }
    

    I think it's clear enough not to need any extra description.

    Approach Two: if you need an input box that user/player can enter string with keyboard and you get what is entered, I recommend to use TextField which is available in cocos2d v3 ( and with some difficulty in v2) and has a full functionality. You can create and initial one of them as:

    auto textField = cocos2d::ui::TextField::create("hint: enter here","Arial" , 30);
    textField->setTextHorizontalAlignment(cocos2d::TextHAlignment::CENTER);
    textField->setTextVerticalAlignment(cocos2d::TextVAlignment::CENTER);
    textField->setColor(Color3B(100,100,100));
    textField->setMaxLength(10);
    textField->setMaxLengthEnabled(true);
    textField->setTouchAreaEnabled(true);
    textField->setTouchSize(Size(200,400));
    textField->setPosition(...);
    textField->addEventListener(CC_CALLBACK_2(HelloWorld::textFieldEvent, this));
    this->addChild(textField, 10);
    

    You can get entered data any time with std::string enteredData= textField->getString();

    You can also do something when user entering text with two event as :

    void HelloWorld::textFieldEvent(Ref *pSender, cocos2d::ui::TextField::EventType type)
    {
        switch (type)
        {
        case cocos2d::ui::TextField::EventType::ATTACH_WITH_IME:
        {
            textField->setColor(Color3B::BLACK);
            // or whatever elese
            break;
        }
        case cocos2d::ui::TextField::EventType::DETACH_WITH_IME:
        {
            textField->setColor(Color3B(100,100,100));
            // or whatever elese
            break;
        }
        }
    }
    

    Enjoy !