Cocos2d-x TextField的使用

前端之家收集整理的这篇文章主要介绍了Cocos2d-x TextField的使用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

cocos2d::ui::Text* _displayValueLabel;

@H_502_5@

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

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

@H_502_5@

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

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

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

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

default:@H_502_5@ break;@H_502_5@ }@H_502_5@ }



猜你在找的Cocos2d-x相关文章