CCTextFieldTTF是一个简单的封装,用起来不是那么便利,在cocos2dx的extension里,对输入框有更加简单使用的类,那就是CCEditBox
上代码先:
bool EditBox01::init(){ // // 1. super init first if ( !MenuLayer::init() ) { return false; } CCSize size = __winSize; m_pEditName = CCEditBox::create(CCSizeMake(size.width-100, 60), CCScale9Sprite::create("extensions/green_edit.png")); m_pEditName->setPosition(ccp(size.width/2, size.height/2)); addChild(m_pEditName); m_pEditName->setFontSize(25); m_pEditName->setFontColor(ccRED); m_pEditName->setPlaceHolder("Name:"); m_pEditName->setPlaceholderFontColor(ccWHITE); m_pEditName->setMaxLength(8); m_pEditName->setReturnType(kKeyboardReturnTypeDone); m_pEditPassword = CCEditBox::create(CCSizeMake(size.width-100, 60), CCScale9Sprite::create("extensions/orange_edit.png")); m_pEditPassword->setPosition(ccp(size.width/2, size.height/2-80)); addChild(m_pEditPassword); m_pEditPassword->setFontColor(ccGREEN); m_pEditPassword->setPlaceHolder("Password:"); m_pEditPassword->setMaxLength(6); m_pEditPassword->setInputFlag(kEditBoxInputFlagPassword); m_pEditPassword->setInputMode(kEditBoxInputModeSingleLine); m_pEditEmail = CCEditBox::create(CCSizeMake(size.width-100, 60), CCScale9Sprite::create("extensions/yellow_edit.png")); m_pEditEmail->setPosition(ccp(size.width/2, size.height/2-80-80)); m_pEditEmail->setPlaceHolder("Email:"); m_pEditEmail->setInputMode(kEditBoxInputModeEmailAddr); addChild(m_pEditEmail); return true;}
三个控件对应上面的代码,当鼠标点击某一个控件时,自动弹出虚拟键盘,并临时调整控件位置,如果需要的话