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

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Récemment, de nombreux utilisateurs de Win11 ont rencontré le problème selon lequel la boîte de dialogue d'expérience de saisie scintille toujours et ne peut pas être désactivée. Cela est en fait dû aux services système et aux composants par défaut de Win11. Nous devons d'abord désactiver les services concernés, puis désactiver ceux-ci. service d’expérience de saisie. Résolu, essayons-le ensemble. Comment désactiver l'expérience de saisie dans Win11 : Première étape, cliquez avec le bouton droit sur le menu Démarrer et ouvrez « Gestionnaire des tâches ». Deuxième étape, recherchez les trois processus « CTF Loader », « MicrosoftIME » et « Service Host : Textinput Management Service ». dans l'ordre, faites un clic droit sur "Fin de tâche" "La troisième étape, ouvrez le menu Démarrer, recherchez et ouvrez "Services" en haut. La quatrième étape, recherchez "Textinp" dedans
![L'entrée Windows rencontre un blocage ou une utilisation élevée de la mémoire [Réparer]](https://img.php.cn/upload/article/000/887/227/170835409686241.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
L'expérience de saisie Windows est un service système clé chargé de traiter les entrées utilisateur provenant de divers périphériques d'interface humaine. Il démarre automatiquement au démarrage du système et s'exécute en arrière-plan. Cependant, ce service peut parfois se bloquer automatiquement ou occuper trop de mémoire, ce qui entraîne une réduction des performances du système. Il est donc crucial de surveiller et de gérer ce processus en temps opportun pour garantir l’efficacité et la stabilité du système. Dans cet article, nous expliquerons comment résoudre les problèmes de blocage de l'expérience de saisie Windows ou entraînant une utilisation élevée de la mémoire. Le service Windows Input Experience n'a pas d'interface utilisateur, mais il est étroitement lié à la gestion des tâches système de base et des fonctions liées aux périphériques d'entrée. Son rôle est d'aider le système Windows à comprendre chaque entrée saisie par l'utilisateur.

La barre de recherche est une fonction très utile du système win11, qui peut nous aider à trouver les paramètres, fonctions et services que nous souhaitons. Cependant, certains amis ont rencontré la situation dans laquelle la barre de recherche win11 ne peut pas être saisie. Nous pouvons modifier les données pertinentes dans le registre pour résoudre le problème. Suivons l'éditeur pour le résoudre. Que faire si la barre de recherche Win11 ne peut pas être saisie 1. Tout d'abord, nous pouvons appuyer sur "win+r" sur le clavier pour afficher Run. 2. Entrez ensuite « regedit » et appuyez sur Entrée pour ouvrir l'éditeur de registre. 3. Saisissez ensuite "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Expl dans le chemin ci-dessus

La matrice est une formule que nous utilisons et rencontrons souvent, mais savez-vous comment saisir une matrice sous forme de mot ? De nombreuses personnes n'y ont peut-être jamais été exposées et se sentiront confuses lorsqu'elles l'utiliseront. Nous allons donc partager ici avec vous comment saisir le mot matrice. J'espère que partager cette technique pourra vous apporter aide et inspiration. 1. Tout d’abord, créez et ouvrez un document Word Afin de démontrer l’opération, nous utiliserons un document vierge pour la démonstration. Comme le montre la figure ci-dessous : 2. Si nous voulons saisir une matrice, nous devons trouver le bouton [Insérer] dans la barre de menu. Ce bouton peut insérer de nombreuses options de contenu, telles que des images, etc., le tout à partir de ce menu. bar. 3. Après avoir cliqué sur [Insérer], regardez sur le côté droit des options de notre outil et vous pouvez trouver [Formule], puis cliquez sur [

Énoncé du problème Écrivez un programme C qui prend des entiers séparés par des espaces comme entrée de tableau. SampleExamples entrée 12345 sortie 'Arrayelementsare-'1,2,3,4,5La traduction chinoise de l'explication est la suivante : L'entrée d'explication contient 5 entiers séparés par des espaces. Entrée 997687542356878967343423 Sortie 'Arrayelementsare-'99,76,87,54,23,56,878,967,34,34,23 La traduction chinoise d'Explication est : Explication L'entrée contient 11 entiers séparés par des espaces. Méthode 1 Dans cette méthode, nous remplacerons l'entrée par du vide

En programmation PHP, nous devons parfois limiter les données saisies par l'utilisateur, par exemple déterminer si l'entrée contient uniquement des chiffres et des lettres. Ceci est souvent rencontré dans le développement de projets réels, il est donc très important de maîtriser comment implémenter cette fonction. Cet article explique comment utiliser PHP pour déterminer que l'entrée ne contient que des chiffres et des lettres, et donne des exemples de code spécifiques. Pourquoi devons-nous déterminer que l’entrée ne contient que des chiffres et des lettres ? Lors du développement de sites Web, les données saisies par l'utilisateur peuvent être utilisées pour des fonctions importantes telles que les opérations de base de données et les opérations sur les fichiers si les données saisies par l'utilisateur contiennent des éléments spéciaux.

Solution : 1. Retard du réseau, problèmes de performances logicielles ou matérielles, les solutions incluent la tentative de redémarrage de l'appareil, la mise à jour du logiciel ou l'effacement du cache, etc. 2. Les paramètres de la méthode de saisie sont incorrects, la solution consiste à vérifier les paramètres de la méthode de saisie et à s'assurer qu'ils sont corrects. configuration ; 3. Défaillance du clavier, la solution consiste à vérifier si le clavier fonctionne correctement et à remplacer le clavier si nécessaire. 4. Défaillance du système d'exploitation ou du logiciel, la solution consiste à mettre à jour le système d'exploitation ou le logiciel, ou à désinstaller et réinstaller le clavier. application problématique ; 5. Attaques de virus ou de logiciels malveillants, la solution consiste à utiliser un logiciel antivirus pour l'analyse et la suppression, etc.

1. Ouvrez le logiciel PPT et entrez dans l'interface de fonctionnement. 2. Recherchez l'option d'insertion dans cette interface. 3. Cliquez sur l'option Insérer et recherchez l'option Symbole spécial dans son sous-menu. 4. Cliquez sur l'option Symbole spécial pour faire apparaître la boîte de dialogue Insérer un symbole spécial. 5. Recherchez l'option de symbole mathématique dans cette boîte de dialogue. 6. Cliquez sur l'option Symbole mathématique et recherchez l'option Pas égal au symbole à l'intérieur. 7. Cliquez sur cette option et vous pouvez voir que notre symbole d'inégalité a été saisi dans la zone de saisie.
