码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2dLua3.17.2集成FairyGUI之 lua绑定 getGroup() 调用问题(三)

时间:2020-01-16 18:53:46      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:color   view   ptr   lib   获取   function   func   type   tin   

在用cocosLua 翻写 fairyGUIdemo的C++版本的代码时,发现在写到obj->getGroup()->name 的时候,运行找不到名字;

demo中的C++代码为

    int cnt = _view->numChildren();
    for (int i = 0; i < cnt; i++)
    {
        GObject* obj = _view->getChildAt(i);
        if (obj->getGroup() != nullptr && obj->getGroup()->name.compare("btns") == 0)
            obj->addClickListener(CC_CALLBACK_1(BasicsScene::runDemo, this));
    }

翻写的代码为

    local cnt = _view:numChildren()
    for i = 1, cnt do
        local obj = _view:getChildAt(i - 1)
        if (obj:getGroup() ~= nil and obj:getGroup().name == "btns") then
            obj:addClickListener(handler(self, self.runDemo))
        end
        --end
    end

打印的obj:getGroup().name 始终为nil。于是去修改源码,在libfairygui工程中GObject.h文件添加两行代码,就是为了把name和id作为函数导出,以便在lua中能被获取到。(id我顺手导出,如果你不用,可以不用导出)

    std::string getName() const { return name; };
    std::string getId() const { return id; };

然后打开lua_cocos2dx_fairygui_auto.cpp文件(上一章中导出的文件,可以去看),添加

int lua_cocos2dx_fairygui_GObject_getName(lua_State* tolua_S)
{
    int argc = 0;
    fairygui::GObject* cobj = nullptr;
    bool ok = true;

#if COCOS2D_DEBUG >= 1
    tolua_Error tolua_err;
#endif


#if COCOS2D_DEBUG >= 1
    if (!tolua_isusertype(tolua_S, 1, "fgui.GObject", 0, &tolua_err)) goto tolua_lerror;
#endif

    cobj = (fairygui::GObject*)tolua_tousertype(tolua_S, 1, 0);

#if COCOS2D_DEBUG >= 1
    if (!cobj)
    {
        tolua_error(tolua_S, "invalid ‘cobj‘ in function ‘lua_cocos2dx_fairygui_GObject_getName‘", nullptr);
        return 0;
    }
#endif

    argc = lua_gettop(tolua_S) - 1;
    if (argc == 0)
    {
        if (!ok)
        {
            tolua_error(tolua_S, "invalid arguments in function ‘lua_cocos2dx_fairygui_GObject_getName‘", nullptr);
            return 0;
        }
        std::string ret = cobj->getName();
        lua_pushlstring(tolua_S, ret.c_str(), ret.length());
        return 1;
    }
    luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "fgui.GObject:getName", argc, 0);
    return 0;

#if COCOS2D_DEBUG >= 1
    tolua_lerror:
                tolua_error(tolua_S, "#ferror in function ‘lua_cocos2dx_fairygui_GObject_getName‘.", &tolua_err);
#endif

                return 0;
}

int lua_cocos2dx_fairygui_GObject_getId(lua_State* tolua_S)
{
    int argc = 0;
    fairygui::GObject* cobj = nullptr;
    bool ok = true;

#if COCOS2D_DEBUG >= 1
    tolua_Error tolua_err;
#endif


#if COCOS2D_DEBUG >= 1
    if (!tolua_isusertype(tolua_S, 1, "fgui.GObject", 0, &tolua_err)) goto tolua_lerror;
#endif

    cobj = (fairygui::GObject*)tolua_tousertype(tolua_S, 1, 0);

#if COCOS2D_DEBUG >= 1
    if (!cobj)
    {
        tolua_error(tolua_S, "invalid ‘cobj‘ in function ‘lua_cocos2dx_fairygui_GObject_getId‘", nullptr);
        return 0;
    }
#endif

    argc = lua_gettop(tolua_S) - 1;
    if (argc == 0)
    {
        if (!ok)
        {
            tolua_error(tolua_S, "invalid arguments in function ‘lua_cocos2dx_fairygui_GObject_getId‘", nullptr);
            return 0;
        }
        std::string ret = cobj->getId();
        lua_pushlstring(tolua_S, ret.c_str(), ret.length());
        return 1;
    }
    luaL_error(tolua_S, "%s has wrong number of arguments: %d, was expecting %d \n", "fgui.GObject:getId", argc, 0);
    return 0;

#if COCOS2D_DEBUG >= 1
    tolua_lerror:
                tolua_error(tolua_S, "#ferror in function ‘lua_cocos2dx_fairygui_GObject_getId‘.", &tolua_err);
#endif

                return 0;
}

并导出两个函数,找到int lua_register_cocos2dx_fairygui_GObject(lua_State* tolua_S) 函数在里面写入

int lua_register_cocos2dx_fairygui_GObject(lua_State* tolua_S)
{
...
...

tolua_function(tolua_S, "getName", lua_cocos2dx_fairygui_GObject_getName);
tolua_function(tolua_S, "getId", lua_cocos2dx_fairygui_GObject_getId);

....
}

至此 已完成导出

下面开始修改lua代码调用

    local cnt = _view:numChildren()
    print("cnt:", cnt)
    for i = 1, cnt do
        local obj = _view:getChildAt(i - 1)
        if (obj:getGroup() ~= nil and obj:getGroup():getName() == "btns") then
            print("name==>::", obj:getGroup():getName())
            obj:addClickListener(handler(self, self.runDemo))
        end
        --end
    end

 

编译,运行

技术图片

 

 完成!

 

Cocos2dLua3.17.2集成FairyGUI之 lua绑定 getGroup() 调用问题(三)

标签:color   view   ptr   lib   获取   function   func   type   tin   

原文地址:https://www.cnblogs.com/whj0402/p/12202511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!