본문 바로가기

Cocos2d-x v3.17/기본기능

[Cocos2d-x 기본기능]UI 컴포넌트 TextField

반응형

TextField 

 

만약 당신게임의 게이머에게 닉네임을 입력시키게 하고 싶을땐 어디서 입력을 하게 만들것인가?

Cocos2d-x는 TextField객체를 제공하여 이런 요구사항을 만족시켜줄수 있다.

이것은 터치이벤트,포커스,위치 고정비율 등을 지원한다. 

 

C++ 

#include "ui/CocosGUI.h" 

auto textField = TextField::create("","Arial",30); 

textField->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){

std::cout << "editing a TextField" << std::endl;

}); 

this->addChild(textField); 

 

위의 예에서 TextField를 생성하고 콜백함수를 지정해줬다. 

 

우리가 제공하는 텍스트필트 객체는 기능이 많고 모든 입력처리를 할수있다.

예를들면 유저ID와 PW를 입력해야 할때 입력가능한 글자 등등을 지원한다. 

 

예를 한번 보자 

C++ 

#include "ui/CocosGUI.h" 

auto textField = TextField::create("","Arial",30); 

// make this TextField password enabled

textField->setPasswordEnabled(true); 

// set the maximum number of characters the user can enter for this TextField

textField->setMaxLength(10); 

textField->addTouchEventListener([&](Ref* sender, Widget::TouchEventType type){

std::cout << "editing a TextField" << std::endl;

}); 

this->addChild(textField); 

 

스크린상에는 텍스트필드는 아래와 같다.

텍스트필드를 클릭하면 가상 키보드가 자동으로 올라온다.이때 문자를 입력할수 있다.

출처: <http://cocos2d-x.org/docs/cocos2d-x/zh/ui_components/textfields.html


반응형