博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第9月第13天 传递lua匿名函数到c/c++
阅读量:4319 次
发布时间:2019-06-06

本文共 4245 字,大约阅读时间需要 14 分钟。

1.

lua函数都在refid_fun

refid_fun[refid] = fun
 
TOLUA_API int toluafix_ref_function(lua_State* L, int lo, int def){    // function at lo    if (!lua_isfunction(L, lo)) return 0;    s_function_ref_id++;    lua_pushstring(L, TOLUA_REFID_FUNCTION_MAPPING);    lua_rawget(L, LUA_REGISTRYINDEX);                           /* stack: fun ... refid_fun */    lua_pushinteger(L, s_function_ref_id);                      /* stack: fun ... refid_fun refid */    lua_pushvalue(L, lo);                                       /* stack: fun ... refid_fun refid fun */    lua_rawset(L, -3);                  /* refid_fun[refid] = fun, stack: fun ... refid_ptr */    lua_pop(L, 1);                                              /* stack: fun ... */    return s_function_ref_id;    // lua_pushvalue(L, lo);                                           /* stack: ... func */    // return luaL_ref(L, LUA_REGISTRYINDEX);}
static int tolua_Cocos2d_CCScriptEventDispatcher_addNodeEventListener00(lua_State* tolua_S){#ifndef TOLUA_RELEASE tolua_Error tolua_err; if (     !tolua_isusertype(tolua_S,1,"CCScriptEventDispatcher",0,&tolua_err) ||     !tolua_isnumber(tolua_S,2,0,&tolua_err) ||     (tolua_isvaluenil(tolua_S,3,&tolua_err) || !toluafix_isfunction(tolua_S,3,"LUA_FUNCTION",0,&tolua_err)) ||     !tolua_isnumber(tolua_S,4,1,&tolua_err) ||     !tolua_isnoobj(tolua_S,5,&tolua_err) )  goto tolua_lerror; else#endif {  CCScriptEventDispatcher* self = (CCScriptEventDispatcher*)  tolua_tousertype(tolua_S,1,0);  int event = ((int)  tolua_tonumber(tolua_S,2,0));  LUA_FUNCTION listener = (  toluafix_ref_function(tolua_S,3,0));  int tag = ((int)  tolua_tonumber(tolua_S,4,0));#ifndef TOLUA_RELEASE  if (!self) tolua_error(tolua_S,"invalid 'self' in function 'addScriptEventListener'", NULL);#endif  {   int tolua_ret = (int)  self->addScriptEventListener(event,listener,tag);   tolua_pushnumber(tolua_S,(lua_Number)tolua_ret);  } } return 1;#ifndef TOLUA_RELEASE tolua_lerror: tolua_error(tolua_S,"#ferror in function 'addNodeEventListener'.",&tolua_err); return 0;#endif}

 

比如;lua代码item:addNodeEventListener,CCScriptEventDispatcher* self===item,int event===cc.MENU_ITEM_CLICKED_EVENT,

LUA_FUNCTION listener =
function(tag)     if sound then audio.playSound(sound) end     listener(tag) end

 

 

--[[--创建一个文字标签菜单项,并返回 CCMenuItemLabel 对象。可用参数:-   listener: 回调函数-   tag: 按钮的 Tag,会传入回调函数。多个按钮使用同一个回调函数时,可根据 Tag 区分哪一个按钮被按下(可选)-   x, y: 坐标(可选)-   sound: 按钮按下时播放什么音效(可选)以及所有可以用于 ui.newTTFLabel() 的参数。@param table params 参数表格对象@return CCMenuItemLabel CCMenuItemLabel对象]]function ui.newTTFLabelMenuItem(params)    local p = clone(params)    p.x, p.y = nil, nil    local label = ui.newTTFLabel(p)    local listener = params.listener    local tag      = params.tag    local x        = params.x    local y        = params.y    local sound    = params.sound    local item = CCMenuItemLabel:create(label)    if item then        if type(listener) == "function" then            item:addNodeEventListener(cc.MENU_ITEM_CLICKED_EVENT, function(tag)                if sound then audio.playSound(sound) end                listener(tag)            end)        end        if x and y then item:setPosition(x, y) end        if tag then item:setTag(tag) end    end    return itemend    self.item1 = ui.newTTFLabelMenuItem({text = "计时模式", size = 30, align = ui.TEXT_ALIGN_CENTER,         x = display.cx, y = display.cy + 40, color = display.COLOR_GREEN,         listener = function()             app:setData("currentLv", 1)            app:enterScene("MainScene",1)        end})

 

int CCLuaStack::executeFunctionByHandler(int nHandler, int numArgs){    int ret = 0;    if (pushFunctionByHandler(nHandler))                                /* L: ... arg1 arg2 ... func */    {        if (numArgs > 0)        {            lua_insert(m_state, -(numArgs + 1));                        /* L: ... func arg1 arg2 ... */        }        ret = executeFunction(numArgs);    }    return ret;}

 

http://blog.csdn.net/linchaolong/article/details/37657077

 

http://www.cppblog.com/kevinlynx/archive/2011/04/24/144905.html

 

http://blog.csdn.net/rain_qingtian/article/details/48573981

 

http://blog.csdn.net/lightxm/article/details/38435067

http://www.cnblogs.com/boliu/p/4091274.html

 

转载于:https://www.cnblogs.com/javastart/p/7001466.html

你可能感兴趣的文章
full page screen capture in js
查看>>
Python基础之re模块(正则表达式)
查看>>
STAT 440 - Spring 2019 - Midterm Project
查看>>
楼主错题:解析
查看>>
RabbitMQ 启用页面管理功能并设置权限
查看>>
C#获取本机局域网ip和公网ip
查看>>
asp.net如何删除文件夹及文件内容操作
查看>>
Problem Best Time to Buy and Sell Stock I
查看>>
LeetCode(79): 单词搜索
查看>>
PHP7三元运算符 ?? 和 ?: 的区别
查看>>
解决win7 Windows USB无法驱动/驱动错误/该设备无法启动。(代码10)
查看>>
代理模式
查看>>
form表单发送请求实例
查看>>
微软正式提供Visual Studio 2013正式版下载(附直接链接汇总)
查看>>
symbian系统开发教程(一)
查看>>
SpringBoot配置属性之Server
查看>>
MySQL 8.0 Docker使用注解
查看>>
题解报告:hdu 2093 考试排名
查看>>
U - Relatives(欧拉函数)
查看>>
【原创】Lucene.Net+盘古分词器(详细介绍)
查看>>