Lua Corona widget.newButton将数据传递给函数

Lua Corona widget.newButton将数据传递给函数,lua,widget,coronasdk,Lua,Widget,Coronasdk,我在文档和谷歌上搜索了一个解决方案,但没有找到 简单地说:我想在按下按钮时将数据传递给函数。 问题:按钮是动态创建的,因此在构建按钮时必须显示数据 代码如下所示 myButton = widget.newButton({ x = width * 0.875, y = height * heightChange, width = width * 0.18, height = height * 0.09, def

我在文档和谷歌上搜索了一个解决方案,但没有找到

简单地说:我想在按下按钮时将数据传递给函数。 问题:按钮是动态创建的,因此在构建按钮时必须显示数据

代码如下所示

    myButton = widget.newButton({
        x = width * 0.875,
        y = height * heightChange,
        width = width * 0.18,
        height = height * 0.09,
        defaultFile = "Assets/Images/button_up.png",
        overFile = "Assets/Images/button_down.png",
        onEvent = func_myFunction
    })
但是我想做一些事情,比如

    onEvent = func_myFunction("My Data")


两者都不起作用。有什么想法吗?

很简单,您只需要在代码末尾添加一个eventlistener,这样当它被单击时,您的数据就会执行

myButton = widget.newButton({
    x = width * 0.875,
    y = height * heightChange,
    width = width * 0.18,
    height = height * 0.09,
    defaultFile = "Assets/Images/button_up.png",
    overFile = "Assets/Images/button_down.png",
    onEvent = func_myFunction
})
myButton:addEventListener("touch",onEvent);

我希望这能解决您的问题。

很简单,您只需要在代码末尾添加一个eventlistener,这样当单击它时,数据就会执行

myButton = widget.newButton({
    x = width * 0.875,
    y = height * heightChange,
    width = width * 0.18,
    height = height * 0.09,
    defaultFile = "Assets/Images/button_up.png",
    overFile = "Assets/Images/button_down.png",
    onEvent = func_myFunction
})
myButton:addEventListener("touch",onEvent);
我希望这能解决你的问题