Corona sdk将表连接到其他lua文件。错误的参数#-1 newText

时间:2017-01-02 08:08:23

标签: lua corona lua-table

我对我的桌子有问题。我无法将表连接到我的其他lua文件。我在本地lblGiven = display.newText上有错误。它显示Error bad argument#-1 newText。该程序的机制是,如果单击该按钮,表格的一部分将显示为标签。

这是我的文件,名为questions.lua

local M = {

{
    "What is the answer 1", 
    answer = "17",

},
{
    "What is the answer 2", 
    answer = "18",

},
{   
    "What is the answer 3", 
    answer = "25",

},
},  

return M

这是我的main.lua

local given = require("questions")

 local lblGiven = display.newText(
    {
        text = given[math.random(#given)],
        x = 160,
        y = 310,
        font = native.systemFont,
        align = "center"
    }
)

1 个答案:

答案 0 :(得分:1)

尝试

local M = {

{
    "What is the answer 1", 
    answer = "17",

},
{
    "What is the answer 2", 
    answer = "18",

},
{   
    "What is the answer 3", 
    answer = "25",

},
} -- remove comma

return M

local given = require("questions")

 local lblGiven = display.newText(
    {
        text = given[math.random(#given)][1],
        x = 160,
        y = 310,
        font = native.systemFont,
        align = "center"
    }
)

说明given[math.random(#given)]为您提供表格。例如,given[1]等于{"What is the answer 1", answer = "17"}。要获得唯一的问题,您需要使用方括号和索引。阅读Understanding lua tables in corona sdk中有关表格​​的更多信息。