cocos2dx之创建一个输入框

在游戏中,有时候修要用户输入用户名密码等,这时候需要用到虚拟键盘来实现,在Cocos2dx中,通过使用集成输入法代理类(CCIMEDelegate)和其字标签类(CCLabelTTF)的输入框类(CCTextFieldTTF)来实现。

例子代码如下:

class KeyboardNotificationLayer;

class MyTextInputLayer : public MyTextInputLayerBase
{
public:
    MyTextInputLayer();
    virtual ~MyTextInputLayer();

public:
    virtual void initLayer();

private:
    KeyboardNotificationLayer *m_keyBoardNotify;

};//end of MyTextInputLayer

/*
*@KeyboardNotificationLayer
*/
class KeyboardNotificationLayer : public MyTextInputLayerBase, public CCIMEDelegate
{
public:
    KeyboardNotificationLayer();
    ~KeyboardNotificationLayer();
    bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
    void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
    void onEnter();
    void onExit();
public:
    void keyboardWillShow(CCIMEKeyboardNotificationInfo& info);
    void keyboardDidShow(CCIMEKeyboardNotificationInfo& info);
    void keyboardWillHide(CCIMEKeyboardNotificationInfo& info);
    void keyboardDidHide(CCIMEKeyboardNotificationInfo& info);
    void registerWithTouchDispatcher(void);
    void onClickTrackNode(bool bClicked);

public:
    void initLayer();

private:
     CCNode * m_pTrackNode;

};// end of MyTextInputLayer


具体实现(代码我写的很不规范,只是举个例子而已):

void KeyboardNotificationLayer::onClickTrackNode(bool bClicked) {
    CCTextFieldTTF * pTextField = dynamic_cast<CCTextFieldTTF *>(m_pTrackNode);
    if(bClicked) {
        pTextField->attachWithIME();
    } else {
        pTextField->detachWithIME();
    }
}

bool KeyboardNotificationLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {
    CCTextFieldTTF * pTextField = dynamic_cast<CCTextFieldTTF *>(m_pTrackNode);
    CCPoint screeView = pTouch->locationInView();
    CCPoint glView = CCDirector::sharedDirector()->convertToGL(screeView);
    if(pTextField != 0) {
        CCSize textSize = pTextField->getContentSize();
        pTextField->attachWithIME();
        CCPoint point = convertTouchToNodeSpaceAR(pTouch);
        CCRect rect = getRect(pTextField);
        this->onClickTrackNode(CCRect::CCRectContainsPoint(rect, point));// 判断点击的位置是不是在输入框的范围之类
    }
    return true;
}

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;
}


创建成功了,你这时候,只要点击屏幕就可以开始输入了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是一个使用Cocos2d创建的简单游戏的示例代码: ```c++ #include "cocos2d.h" USING_NS_CC; class HelloWorld : public cocos2d::Scene { public: static cocos2d::Scene* createScene(); virtual bool init(); void menuCloseCallback(cocos2d::Ref* pSender); CREATE_FUNC(HelloWorld); }; Scene* HelloWorld::createScene() { auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init() { if (!Scene::init()) { return false; } auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24); label->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - label->getContentSize().height)); this->addChild(label, 1); auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width / 2, origin.y + closeItem->getContentSize().height / 2)); auto menu = Menu::create(closeItem, NULL); menu->setPosition(Vec2::ZERO); this->addChild(menu, 1); auto sprite = Sprite::create("HelloWorld.png"); sprite->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y)); this->addChild(sprite, 0); return true; } void HelloWorld::menuCloseCallback(Ref* pSender) { Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif } int main(int argc, char** argv) { // 创建应用程序实例 auto application = Application::getInstance(); // 设置设计分辨率和屏幕分辨率匹配 application->setApplicationName("HelloWorld"); application->setCommandLineArguments(argc, argv); // 创建并运行场景 auto scene = HelloWorld::createScene(); application->run(); return 0; } ``` 这是一个简单的 "Hello World" 游戏示例,使用了Cocos2d库来创建一个场景,并在场景中显示一段文字和一个关闭按钮。你可以根据你的需求进行扩展和修改。 请确保你已经安装了Cocos2d库,并配置好开发环境。在编译和运行之前,请确保已将相关资源文件(如字体文件、图片文件等)放置在正确的路径下,并在代码中正确引用它们。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值