现在的位置: 首页 > 综合 > 正文

初学试试看cocos2dx的TestCPP框架(9)—TextInputTestScene

2014年09月06日 ⁄ 综合 ⁄ 共 1884字 ⁄ 字号 评论关闭

这个测试比较少,就2个方面测试。先看UML图。

一、TextFieldTTFDefaultTest类他继承自KeyboardNotificationLayer类。KeyboardNotificationLayer先设置setTouchEnable(true), 然后重载了registerWithTouchDispatchar

注册CCTouchDispachar来让Layer处理Touch事件。

KeyboardNotificationLayer::KeyboardNotificationLayer()
: m_pTrackNode(0)
{
    setTouchEnabled(true);
}

void KeyboardNotificationLayer::registerWithTouchDispatcher()
{
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
}

这个类里面有段代码可以显示提取这个层里面的子字符串出来,然后处理

CCArray * children = getChildren();
CCNode * node = 0;
int count = children->count();
for (int i = 0; i < count; i++) {
   node = (CCNode*)children->ObjectAtIndex(i);
   ........................
}

另外这个类重载了ccTouchBegan、ccTouchEnded2个方法,ccTouchEnded调用需函数onClickTrackNode做各种的处理。

然后子类在onClickTrackNode里面获取跟踪的CCTextFieldTTF指针,CCTextFieldTTF 的指针pTextField->attachWithIME();

二、先看看这个类的方法

    void callbackRemoveNodeWhenDidAction(CCNode * pNode);

    // KeyboardNotificationLayer
    virtual std::string subtitle();
    virtual void onClickTrackNode(bool bClicked);

    // CCLayer
    virtual void onEnter();
    virtual void onExit();

    // 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);

先在onEnter,用CCTextFieldTTF::textFieldWithPlaceHolder创建CCTextFieleTTF,和浅入浅出的动作。点击这个文字。先调用ccTouchBegan,然后ccTouchEnded,在

ccTouchEnded里面调用需函数OnClickTrackNode,然后在里面attch/WithIME,attchWithIME里面调用attachDelegateWithIME,attachDelegateWithIME里面调用

canAttachWithIME,canAttachWithIME里面调用子类的onTextFieldAttachWithIME,当前代码的里面的onTextFieldAttachWithIME仅是执行淡入淡出的动作。

离开的时候调用onTextFieldDetachWithIME,里面仅是执行stopAction和设置一下透明度setOpacity。

输入字符的时候执行onTextFieldInsertText动作,删除字符的时执行候onTextFieldDeleteBackward里面的动作。onDraw直接返回。

抱歉!评论已关闭.