Cocos2d-x TextField的使用

void textFieldEvent(Ref *pSender, cocos2d::ui::TextField::EventType type);

cocos2d::ui::Text*  _displayValueLabel;


Size size = Director::getInstance()->getVisibleSize();
_displayValueLabel = cocos2d::ui::Text::create("No Event", "Marker Felt.ttf", 32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
_displayValueLabel->setPosition(Vec2(size.width / 2.0f, size.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
this->addChild(_displayValueLabel);


cocos2d::ui::TextField* textField = cocos2d::ui::TextField::create("input words here", "Arial", 30);
textField->setMaxLengthEnabled(true);
textField->setMaxLength(3);
textField->setPosition(Vec2(size.width / 2.0f, size.height / 2.0f));
textField->addEventListener(CC_CALLBACK_2(HelloWorld::textFieldEvent, this));
this->addChild(textField);


void HelloWorld::textFieldEvent(Ref *pSender, cocos2d::ui::TextField::EventType type)
{
 switch (type)
 {
 case cocos2d::ui::TextField::EventType::ATTACH_WITH_IME:
 {
  cocos2d::ui::TextField* textField = dynamic_cast(pSender);
  Size screenSize = CCDirector::getInstance()->getWinSize();
  textField->runAction(CCMoveTo::create(0.225f,
   Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
  _displayValueLabel->setString(String::createWithFormat("attach with IME max length %d", textField->getMaxLength())->getCString());
 }
 break;

 case cocos2d::ui::TextField::EventType::DETACH_WITH_IME:
 {
  cocos2d::ui::TextField* textField = dynamic_cast(pSender);
  Size screenSize = CCDirector::getInstance()->getWinSize();
  textField->runAction(CCMoveTo::create(0.175f, Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
  _displayValueLabel->setString(String::createWithFormat("detach with IME max length %d", textField->getMaxLength())->getCString());
 }
 break;

 case cocos2d::ui::TextField::EventType::INSERT_TEXT:
 {
  TextField* textField = dynamic_cast(pSender);
  _displayValueLabel->setString(String::createWithFormat("insert words max length %d", textField->getMaxLength())->getCString());
 }
 break;

 case cocos2d::ui::TextField::EventType::DELETE_BACKWARD:
 {
  TextField* textField = dynamic_cast(pSender);
  _displayValueLabel->setString(String::createWithFormat("delete word max length %d", textField->getMaxLength())->getCString());
 }
 break;

 default:
  break;
 }
}



你可能感兴趣的:(Cocos2d-x,3.4,学习)