Lua发送一个被调用的函数';姓名
基本上我想知道什么时候起作用Lua发送一个被调用的函数';姓名,lua,agen-framework,Lua,Agen Framework,基本上我想知道什么时候起作用 button[n]:onclick() --where n can be any string value function allbuttons(n) --where n is the n from button[n]:onclick() function button['options']:onclick() allbuttons('options') end function button['quit']:onclick() allbuttons(
button[n]:onclick() --where n can be any string value
function allbuttons(n) --where n is the n from button[n]:onclick()
function button['options']:onclick()
allbuttons('options')
end
function button['quit']:onclick()
allbuttons('quit')
end
(...)
function button[n]:onclick()
allbuttons(n)
end
被调用,并将其名称(特别是我想发送“n”)发送到另一个函数
button[n]:onclick() --where n can be any string value
function allbuttons(n) --where n is the n from button[n]:onclick()
function button['options']:onclick()
allbuttons('options')
end
function button['quit']:onclick()
allbuttons('quit')
end
(...)
function button[n]:onclick()
allbuttons(n)
end
然后处理所有可能的请求。我之所以要这样做,是因为指定了按钮[n],因此每次单击按钮时都会触发按钮[n]:onclick(),但每次我希望在大allbuttons函数中处理另一个buttonclick时,编写这些函数似乎是不对的
button[n]:onclick() --where n can be any string value
function allbuttons(n) --where n is the n from button[n]:onclick()
function button['options']:onclick()
allbuttons('options')
end
function button['quit']:onclick()
allbuttons('quit')
end
(...)
function button[n]:onclick()
allbuttons(n)
end
我试过这样的方法
debug.sethook(allbuttons, 'c')
function allbuttons()
n = debug.getinfo(2).name
end
但是我想我并不完全理解如何使用debug.sethook..设置
按钮[n]:onclick
成为您想要的函数(所有按钮),除了这里有一个棘手的部分,值n。你可能已经知道你能做到
button[n].onclick = allbuttons
但是如果事件调度器调用onclick
作为button[n]:onclick()
,那么allbuttons
将始终获取button作为第一个参数。如果您真正想在allbuttons
中了解单击的button[n]
实例,则只需将allbuttons(n)
的定义更改为allbuttons(button)
,并相应地更改其代码
如果您需要n,但无法以任何其他方式使用,则可以创建一个匿名闭包,并将访问n的权限作为upvalue(有关详细信息,请参阅):
或者你也可以这样做:
function getAllbuttonsN(n)
return function (self)
allbuttons(n)
end
end
button[1].onclick = getAllbuttonsN(1)
代码更简单,但索引在表达式中出现两次,这可能是错误源 不,这是代理(),您不能这样做,但您可以这样做
函数定义\u onclick(b,n)b[n]。onclick=function()所有按钮(n)end end end define\u onclick(按钮,'options')定义\u onclick(按钮,'quit')
按钮[n]:onclick=
无效。您需要button[n].onclick=
第二个选项对我有效,我现在使用button[n].onclick=function()allbuttons(n)end,看起来也是这样。谢谢@Alistaire您的意思是按钮[1]。onclick=function()所有按钮(1)结束,依此类推?那当然也行。但是,您正在复制一堆代码。除非n作为一个upvalue可用,例如在一个函数中,您为button表中的每个字段调用它(这实际上相当于第一种技术),否则我实际上是在一个button表中循环。