Cant move the layout up when keyboard presented

355 Views Asked by At

I'm trying to move the layout up when the keyboard is presented cause it hides my input textfield but my app crash from line HASH_FIND_PTR(_targets, &tmp, element); from this class

void ActionManager::addAction(Action *action, Node *target, bool paused)
{
    CCASSERT(action != nullptr, "");
    CCASSERT(target != nullptr, "");

    tHashElement *element = nullptr;
    // we should convert it to Ref*, because we save it as Ref*
    Ref *tmp = target;
    HASH_FIND_PTR(_targets, &tmp, element); //error
    if (! element)
    {
        element = (tHashElement*)calloc(sizeof(*element), 1);
        element->paused = paused;
        target->retain();
        element->target = target;
        HASH_ADD_PTR(_targets, target, element);
    }

     actionAllocWithHashElement(element);

     CCASSERT(! ccArrayContainsObject(element->actions, action), "");
     ccArrayAppendObject(element->actions, action);

     action->startWithTarget(target);
}

I'm using cocos2d-x 3.2 i have debug and it's crashing after this line m_pLayout->runAction(moveBy); anyone please give me some advice how can i solve it thanks

My LoginScene.h

#include "cocos2d.h"
#include "cocos-ext.h"
#include "CocosGUI.h"

USING_NS_CC;
USING_NS_CC_EXT;
using namespace ui;

class LoginScene : public Scene
{
public:
    LoginScene(bool pPortrait=false);
    ~LoginScene();

    virtual void onEnter();
    virtual void onExit();

    void onLogin(Ref* pSender, Widget::TouchEventType type);
    void onRegister(Ref* pSender, Widget::TouchEventType type);
    void TextFieldEvent(Ref* pSender,TextField::EventType type);

protected:
    Layout* m_pLayout;
    Layer* m_pUILayer;
};

and LoginScene.cpp

#include "LoginScene.h"
#include "cocostudio/CCSSceneReader.h"
#include "cocostudio/CCSGUIReader.h"
#include "cocostudio/CCActionManagerEx.h"
#include <sqlite3.h>

LoginScene::LoginScene(bool pPortrait):m_pLayout(NULL),m_pUILayer(NULL)
{
    Scene::init();
}

LoginScene::~LoginScene()
{

}

void LoginScene::onEnter()
{
    Scene::onEnter();

    m_pUILayer=Layer::create();
    m_pUILayer->scheduleUpdate();
    addChild(m_pUILayer);

    //register root from json
    m_pLayout=dynamic_cast<Layout*>(cocostudio::GUIReader::getInstance()->widgetFromJsonFile("LoginScene/LoginScene.json"));
    m_pUILayer->addChild(m_pLayout);

    //button initialize
    Button *btnLogin=static_cast<Button*>(Helper::seekWidgetByName(m_pLayout, "btnLogin"));
    btnLogin->addTouchEventListener(CC_CALLBACK_2(LoginScene::onLogin, this));

    Button *btnRegister=static_cast<Button*>(Helper::seekWidgetByName(m_pLayout, "btnRegister"));
    btnRegister->addTouchEventListener(CC_CALLBACK_2(LoginScene::onRegister, this));

    //textfield initialize
    TextField *txtUsername=static_cast<TextField*>(Helper::seekWidgetByName(m_pLayout, "txtUsername"));
    txtUsername->addEventListenerTextField(m_pLayout, textfieldeventselector(LoginScene::TextFieldEvent));

    TextField *txtPassword=static_cast<TextField*>(Helper::seekWidgetByName(m_pLayout, "txtPassword"));
    txtPassword->addEventListenerTextField(m_pLayout, textfieldeventselector(LoginScene::TextFieldEvent));

}

void LoginScene::onExit()
{
    m_pUILayer->removeFromParent();

    cocostudio::GUIReader::destroyInstance();
    cocostudio::ActionManagerEx::destroyInstance();
    cocostudio::SceneReader::destroyInstance();

    Scene::onExit();
}

void LoginScene::onLogin(Ref* pSender, Widget::TouchEventType type)
{
    if(type==Widget::TouchEventType::ENDED)
    {
        CCLOG("abc");

    }
}

void LoginScene::onRegister(Ref* pSender, Widget::TouchEventType type)
{
    if (type==Widget::TouchEventType::ENDED)
    {
        CCLOG("abc");
    }
}

void LoginScene::TextFieldEvent(Ref* pSender,TextField::EventType type)
{
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    switch (type)
    {
        case TextField::EventType::ATTACH_WITH_IME:
        {
            TextField* txtUsername=dynamic_cast<TextField*>(pSender);
            MoveBy* moveBy=MoveBy::create(0.5f, Vec2(0,txtUsername->getContentSize().height*2.5));
            m_pLayout->runAction(moveBy);
        }
            break;

        case TextField::EventType::DETACH_WITH_IME:
        {
            TextField* txtUsername=dynamic_cast<TextField*>(pSender);
            MoveBy* moveBy=MoveBy::create(0.1f, Vec2(0,-txtUsername->getContentSize().height*2.5));
            m_pLayout->runAction(moveBy);
        }
            break;

        default:
            break;
    }
    #endif
}
0

There are 0 best solutions below