如果我正确理解了这个问题,您希望能够从 on_click 处理程序中引用该on_click
处理程序所属的对象。为此,您需要拆分您拥有的语句:
local card = { name = "my card" }
local object = {
identifier = "hand:" .. card.name,
area = { x, y, 100, 100 },
}
object.on_click = function()
-- Code goes here
-- you can reference card and object here (they are upvalues in this context)
print(card.name, object.area[3])
end
object.click()
您也可以on_click
稍微不同地定义;在这种情况下,您将获得object
隐式声明的self
变量(请注意,您对它的称呼也有所不同):
function object:on_click()
-- Code goes here
-- you can reference card and object here
print(card.name, self.area[3])
end
object:click() -- this is the same as object.click(object)