1

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
}
  • "got error" is not a problem description. Post the exact error message and point out the line(s) where the error occurs (you have at least two instances of the runaction line) – CodeSmile Aug 20 '14 at 07:54
  • Tk u, I have edited my question and title to make it clear for anyone read – giallorossi Aug 20 '14 at 08:12
  • You did not post the exact error message. Copy it from the IDE and paste it here. – CodeSmile Aug 20 '14 at 09:17
  • Hi, sorry but i'm new so i dont know what do u mean? I compile it success but it stops from this line with this comment exec_bad_access when i click on textfield for input and no error red line so i dont know how to give u the exactly error All i want someone show me the way how to move up the Layout in cocos2d-x when user input textfield – giallorossi Aug 20 '14 at 09:34
  • If you are using Xcode the error is printed in the console (View->Debug Area->Activate Console) along with possibly more information regarding the issue. Given EXC_BAD_ACCESS the error is that either _targets or tmp (aka target) pointers are invalid (dangling) or null in the line raising the error. – CodeSmile Aug 20 '14 at 14:01

0 Answers0