Creating text fields
You may want to set a nickname in your game. To set nicknames or to get the player's input text, you can use the TextField
class. In this recipe, we will learn about a simple TextField
sample and how to add a textfield in a game.
How to do it...
You will create a text field by specifying the placeholder text, font name, and font size. Then, you set a callback function by using addEventListener
. In the callback function, you can get the text that the player input in the textField
. Create the textField
by using the following code:
auto textField = ui::TextField::create("Enter your name", "Arial", 30); textField->setPosition(Vec2(size.width/2, size.height*0.75f)); this->addChild(textField); textField->addEventListener([](Ref* sender, ui::TextField::EventType type){ auto textField = dynamic_cast<ui::TextField*>(sender); switch (type) { case ui::TextField::EventType::ATTACH_WITH_IME: CCLOG("displayed keyboard"); ...