cocos2dx中的输入类CCTextFieldTTF的用法
cocos2dx中的输入类CCTextFieldTTF。还是相当好用的, 其中,很多人都会关注怎么判断用户输入的数字,字母,汉字? 通过重载onTextFieldInsertText函数,我们可以自定义自己想要的效果。 以下代码,是参考官方的示例,添加了是否数字、字母、汉字的判断,还
cocos2dx中的输入类CCTextFieldTTF。还是相当好用的,
其中,很多人都会关注怎么判断用户输入的数字,字母,汉字?
通过重载onTextFieldInsertText函数,我们可以自定义自己想要的效果。
以下代码,是参考官方的示例,添加了是否数字、字母、汉字的判断,还增加了以空格和回车作为输入结束符。
以下代码,拷到新建项目的HelloWorld中可以直接用(本文版本cocos2dx 2.2.2)。
上代码: .h文件
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" USING_NS_CC; class HelloWorld : public cocos2d::CCLayer,public CCTextFieldDelegate,public CCIMEDelegate { public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init(); // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene(); // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(HelloWorld); void callbackRemoveNodeWhenDidAction(CCNode * pNode); virtual void onClickTrackNode(bool bClicked,CCTextFieldTTF * pSender); // CCLayer virtual void onEnter(); virtual void onExit(); virtual void registerWithTouchDispatcher(); virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); // CCTextFieldDelegate virtual bool onTextFieldAttachWithIME(CCTextFieldTTF * pSender); virtual bool onTextFieldDetachWithIME(CCTextFieldTTF * pSender); virtual bool onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen); virtual bool onTextFieldDeleteBackward(CCTextFieldTTF * pSender, const char * delText, int nLen); virtual bool onDraw(CCTextFieldTTF * pSender); //CCIMEDelegate //keyboard show/hide notification //virtual void keyboardWillShow(CCIMEKeyboardNotificationInfo& info); //virtual void keyboardWillHide(CCIMEKeyboardNotificationInfo& info); private: CCTextFieldTTF* m_pTextField; CCTextFieldTTF* m_pTextField2; CCAction* m_pTextFieldAction; bool m_bAction; int m_nCharLimit; // the textfield max char limit CCPoint m_beginPos; float adjustVert; }; #endif // __HELLOWORLD_SCENE_H__
.cpp文件
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" using namespace cocos2d; using namespace CocosDenshion; #define FONT_NAME "Thonburi" #define FONT_SIZE 36 CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } setTouchEnabled(true); //注意要设置当前layer为可触摸 CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite* pSprite = CCSprite::create("HelloWorld.png"); pSprite->setPosition( ccp(size.width/2, size.height/2) ); this->addChild(pSprite, 0); return true; } void HelloWorld::registerWithTouchDispatcher() { CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);//true会吞噬 } void HelloWorld::onEnter() { CCLayer::onEnter(); //这个父类的调用很重要! m_nCharLimit = 12; m_pTextFieldAction = CCRepeatForever::create( CCSequence::create( CCFadeOut::create(0.25), CCFadeIn::create(0.25), NULL )); m_pTextFieldAction->retain(); //这里一定要retain一次,否则会出现内存问题。 m_bAction = false; // add CCTextFieldTTF CCSize s = CCDirector::sharedDirector()->getWinSize(); m_pTextField = CCTextFieldTTF::textFieldWithPlaceHolder("<click here for input>", FONT_NAME, FONT_SIZE); m_pTextField->setColor(ccWHITE); //设置输入编辑框中字符的颜色 // m_pTextField->setSecureTextEntry(true); //输入密码时,用点字符替代 m_pTextField->setDelegate(this); //很重要 勿漏!!! m_pTextField->setPosition(ccp(s.width / 2, s.height / 2+30)); //将输入编辑框的y轴位置设低是为了测试,当出现键盘的时候,输入编辑框的自动向上调整。 addChild(m_pTextField); m_pTextField2 = CCTextFieldTTF::textFieldWithPlaceHolder("<click here for input>", FONT_NAME, FONT_SIZE); m_pTextField2->setColor(ccWHITE); //设置输入编辑框中字符的颜色 // m_pTextField2->setSecureTextEntry(true); //输入密码时,用点字符替代 m_pTextField2->setDelegate(this); m_pTextField2->setPosition(ccp(s.width / 2, s.height / 2-30)); //将输入编辑框的y轴位置设低是为了测试,当出现键盘的时候,输入编辑框的自动向上调整。 addChild(m_pTextField2); } //返回节点的rect static CCRect getRect(CCNode * pNode) { CCRect rc; rc.origin = pNode->getPosition(); rc.size = pNode->getContentSize(); rc.origin.x -= rc.size.width / 2; rc.origin.y -= rc.size.height / 2; return rc; } bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { CCLOG("++++++++++++++++++++++++++++++++++++++++++++"); m_beginPos = pTouch->getLocation(); return true; } void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) { if (! m_pTextField) { return; } CCPoint endPos = pTouch->getLocation(); // 以下这部分代码是用于检测 begin touch 到 end touch之间的距离是否超过5.0,如果是,则返回;否则,继续执行下面的判断是否点击到编辑框的代码。 float delta = 5.0f; if (::abs(endPos.x - m_beginPos.x) > delta || ::abs(endPos.y - m_beginPos.y) > delta) { // not click m_beginPos.x = m_beginPos.y = -1; return; } // decide the trackNode is clicked. CCRect rect; rect = getRect(m_pTextField); this->onClickTrackNode(rect.containsPoint(endPos),m_pTextField); CCRect rect2; rect2 = getRect(m_pTextField2); this->onClickTrackNode(rect2.containsPoint(endPos),m_pTextField2); CCLOG("----------------------------------"); } void HelloWorld::onClickTrackNode(bool bClicked,CCTextFieldTTF * pSender) { if (bClicked) { // TextFieldTTFTest be clicked CCLOG("attachWithIME"); pSender->attachWithIME(); //调用键盘 } else { // TextFieldTTFTest not be clicked CCLOG("detachWithIME"); pSender->detachWithIME(); //隐藏键盘 } } void HelloWorld::onExit() { m_pTextFieldAction->release(); CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this); } // CCTextFieldDelegate protocol bool HelloWorld::onTextFieldAttachWithIME(CCTextFieldTTF * pSender) { if (! m_bAction) { pSender->runAction(m_pTextFieldAction); m_bAction = true; } return false; } bool HelloWorld::onTextFieldDetachWithIME(CCTextFieldTTF * pSender) { if (m_bAction) { pSender->stopAction(m_pTextFieldAction); pSender->setOpacity(255); m_bAction = false; } return false; } //本文的重点在此 bool HelloWorld::onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen) { //if insert enter, treat as default to detach with ime CCLOG("%d",nLen);//当前输入的单个字符长度 //空格和\n作为输入结束符 if (*text==' '||'\n' == *text) { pSender->detachWithIME(); //关闭输入 隐藏键盘 return true; } //中文的nlen是3 数字和字母的是1 //如果输入是中文 则不接受输入的内容 if (nLen>1) { return true;//true 则不接受输入的内容 但是可以继续输入 } //判断是否数字或者字符,和下划线_ //不接受数字和英文大小写字符以外的输入 if((*text>='0'&& *text='a'&&*text='A')&&(*text='_') { } else { return true; } // if the textfield's char count more than m_nCharLimit, doesn't insert text anymore. if (pSender->getCharCount() >= m_nCharLimit) { return true; } //// 创建输入时动画 create a insert text sprite and do some action //CCLabelTTF * label = CCLabelTTF::create(text, FONT_NAME, FONT_SIZE); //this->addChild(label); //ccColor3B color = { 226, 121, 7}; //label->setColor(color); // //// move the sprite from top to position //CCPoint endPos = pSender->getPosition(); //if (pSender->getCharCount()) //{ // endPos.x += pSender->getContentSize().width / 2; //} //CCSize inputTextSize = label->getContentSize(); //CCPoint beginPos(endPos.x, CCDirector::sharedDirector()->getWinSize().height - inputTextSize.height * 2); // //float duration = 0.5; //label->setPosition(beginPos); //label->setScale(8); // //CCAction * seq = CCSequence::create( // CCSpawn::create( // CCMoveTo::create(duration, endPos), // CCScaleTo::create(duration, 1), // CCFadeOut::create(duration), // 0), // CCCallFuncN::create(this, callfuncN_selector(HelloWorld::callbackRemoveNodeWhenDidAction)), // 0); //label->runAction(seq); return false; } bool HelloWorld::onTextFieldDeleteBackward(CCTextFieldTTF * pSender, const char * delText, int nLen) { ////创建删除字符动画 create a delete text sprite and do some action //CCLabelTTF * label = CCLabelTTF::create(delText, FONT_NAME, FONT_SIZE); //this->addChild(label); // //// move the sprite to fly out //CCPoint beginPos = pSender->getPosition(); //CCSize textfieldSize = pSender->getContentSize(); //CCSize labelSize = label->getContentSize(); //beginPos.x += (textfieldSize.width - labelSize.width) / 2.0f; // //CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //CCPoint endPos(- winSize.width / 4.0f, winSize.height * (0.5 + (float)rand() / (2.0f * RAND_MAX))); // //float duration = 1; //float rotateDuration = 0.2f; //int repeatTime = 5; //label->setPosition(beginPos); // //CCAction * seq = CCSequence::create( // CCSpawn::create( // CCMoveTo::create(duration, endPos), // CCRepeat::create( // CCRotateBy::create(rotateDuration, (rand()%2) ? 360 : -360), // repeatTime), // CCFadeOut::create(duration), // 0), // CCCallFuncN::create(this, callfuncN_selector(HelloWorld::callbackRemoveNodeWhenDidAction)), // 0); //label->runAction(seq); return false; } bool HelloWorld::onDraw(CCTextFieldTTF * pSender) { return false; } void HelloWorld::callbackRemoveNodeWhenDidAction(CCNode * pNode) { this->removeChild(pNode, true); } //虚拟键盘 //void HelloWorld::keyboardWillShow(CCIMEKeyboardNotificationInfo& info) //{ // CCLOG("TextInputTest:keyboardWillShowAt(origin:%f,%f, size:%f,%f)", // info.end.origin.x, info.end.origin.y, info.end.size.width, info.end.size.height); // // if (! m_pTextField) // { // return; // } // // CCRect rectTracked = getRect(m_pTextField); // // CCLOG("TextInputTest:trackingNodeAt(origin:%f,%f, size:%f,%f)", // rectTracked.origin.x, rectTracked.origin.y, rectTracked.size.width, rectTracked.size.height); // // // if the keyboard area doesn't intersect with the tracking node area, nothing need to do. // if (! rectTracked.intersectsRect(info.end)) // { // return; // } // // // assume keyboard at the bottom of screen, calculate the vertical adjustment. // // //计算出需要y轴需要调整的距离 // adjustVert = info.end.getMaxY() - rectTracked.getMinY(); // CCLOG("TextInputTest:needAdjustVerticalPosition(%f)", adjustVert); // // // move all the children node of KeyboardNotificationLayer // CCArray * children = getChildren(); // CCNode * node = 0; // int count = children->count(); // CCPoint pos; // for (int i = 0; i objectAtIndex(i); // pos = node->getPosition(); // pos.y += adjustVert; //所有的节点都向上移动 // node->setPosition(pos); // } //} // // //void HelloWorld::keyboardWillHide(CCIMEKeyboardNotificationInfo &info) //{ // CCLOG("TextInputTest:keyboardWillShowAt(origin:%f,%f, size:%f,%f)", // info.end.origin.x, info.end.origin.y, info.end.size.width, info.end.size.height); // // CCArray * children = getChildren(); // CCNode * node = 0; // int count = children->count(); // CCPoint pos; // for (int i = 0; i objectAtIndex(i); // pos = node->getPosition(); // pos.y -= adjustVert; //所有的节点都向下移动,恢复原来的位置 // node->setPosition(pos); // } //}</click></click>
(注意:onTextFieldInsertText函数中是const char * text,使用的时候需要星号* text)
输入框,把锚点设置在(0.0,0.5),则会左对齐,此外如果这个修改了,也需要修改触摸的范围。
我习惯另外做一个显示的背景框,用作点击范围,这样用户使用比较方便。
CCTextFieldTTF相当灵活,方便我们自定义。很好!大赞!
参考资料:
http://blog.csdn.net/crayondeng/article/details/12175367 Cocos2d-x CCEditBox & CCTextFieldTTF

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

最近有很多win11遇到了输入体验对话框总是闪烁,关也关不掉的问题,这其实是由于win11的默认系统服务和组件导致的,我们需要先禁用相关服务,再禁用输入体验服务就可以解决了,下面一起来试试看吧。win11输入体验怎么关闭:第一步,右键开始菜单,打开“任务管理器”第二步,依次找到“CTF加载程序”、“MicrosoftIME”和“服务主机:Textinputmanagementservice”三个进程,右键“结束任务”第三步,打开开始菜单,在上方搜索并打开“服务”第四步,在其中找到“Textinp
![Windows输入遇到挂起或内存使用率高的问题[修复]](https://img.php.cn/upload/article/000/887/227/170835409686241.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
Windows的输入体验是一个关键的系统服务,负责处理来自各种人机接口设备的用户输入。它在系统启动时自动启动,在后台运行。然而,有时候这个服务可能会出现自动挂起或占用过多内存的情况,导致系统性能下降。因此,及时监控和管理这个进程是至关重要的,以确保系统的效率和稳定性。在这篇文章中,我们将分享如何解决Windows输入体验被挂起或导致内存使用率高的问题。Windows输入体验服务没有用户界面,但它与处理与输入设备相关的基本系统任务和功能有密切关联。它的作用是帮助Windows系统理解用户输入的每一

搜索栏是win11系统中非常好用的功能,可以帮助我们找到想要的设置、功能和服务等。但是有的朋友遇到了win11搜索栏无法输入的情况,我们可以在注册表中修改相关数据来解决,下面就跟着小编一起来解决一下吧。win11搜索栏无法输入怎么办1、首先我们可以按下键盘的“win+r”调出运行。2、然后在其中输入“regedit”回车确定打开注册表编辑器。3、然后在上方路径中输入“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Expl

矩阵是我们经常使用和遇到的公式,但是在word里如何输入矩阵你会操作吗?很多人可能没有接触过,遇到使用的时候就会感觉困惑,那么,这里我们就跟大家分享一下word矩阵怎么输入。希望经过这个技巧的分享,能够给你带来帮助和启发。1、首先,新建并打开一个word文档,为了演示操作,我们就使用空白文档进行演示。如下图所示:2、我们要输入矩阵,需要在菜单栏找到【插入】按钮,这个按钮可以插入很多内容选项,比如图片等都是从这个菜单栏完成。3、点击【插入】之后,我们工具选项右侧看,可以找到【公式】,然后我们点击【

问题陈述编写一个C程序,以空格分隔的整数作为数组输入。SampleExamples输入12345输出‘Arrayelementsare-’1,2,3,4,5Explanation的中文翻译为:解释输入包含5个以空格分隔的整数。输入997687542356878967343423输出‘Arrayelementsare-’99,76,87,54,23,56,878,967,34,34,23Explanation的中文翻译为:解释输入包含11个以空格分隔的整数。方法一在这种方法中,我们将把输入中的以空

在PHP编程中,有时候我们需要对用户输入的数据进行限制,比如判断输入是否只包含数字和字母。这在实际项目开发中经常会遇到,因此掌握如何实现这一功能非常重要。本文将介绍如何使用PHP来判断输入只包含数字和字母,并给出具体的代码示例。为什么需要判断输入只包含数字和字母?在网站开发中,用户输入的数据可能会被用于数据库操作、文件操作等重要功能,如果用户输入的数据包含特

Python浮点型输入的注意事项及示例在Python中,浮点数是一种常见的数据类型,用于表示带有小数部分的数值。在进行浮点型输入时,有一些注意事项需要我们了解和注意,以确保输入的正确性和准确性。本文将介绍几个常见的注意事项,并提供示例代码以加深理解。浮点型的输入方式在Python中,浮点型的输入方式有多种,常见的有以下几种:直接使用浮点数进行输入:例如:x

1、打开PPT软件,进入操作界面。2、在这个界面内找到插入选项。3、点击插入选项,在其子级菜单中找到特殊符号选项。4、点击特殊符号选项,弹出插入特殊符号对话框。5、在该对话框内找到数学符号选项。6、点击数学符号选项,在其内找到不等于符号选项。7、点击该选项,可以看到再输入区我们的不等于符号就输入进来了。
