Lua错误:需要字符串,但得到了nil --- 错误的第二个参数

这是一个用Lua撰写的游戏脚本,用于Corona SDK。一开始,(旧的代码)非常低效,我必须手动创建每个数学问题,用(新的代码)在SO上的某人的帮助下,我解决了这个问题。

现在控制台中显示以下错误:

line 93: local questionText = display.newText(questionGroup, questions[currentQuestion].question, 0,0, chalkfFont, 34 )

game.lua:93: bad argument #2 to 'newText' (string expected, got nil)

-- mathQuestions.lua(旧代码)
local M = {}
M["times"] = {
    {
        question =“6 x 5”, - 问题。
        answers = {“30”,“11”,“29”,“20”}, - 可能答案的数组。
        answer = 1 - 上述数组中的正确答案。
    },
}
返回M

-- mathQuestions.lua(新代码)
local rnd = function(x)返回math.random(1,x)end
M.times = {}
local numQuestions = 10  - 数据库中有多少个问题
对于iu = 1,numQuestions do
    local obj =
    {
        left = math.random(1,10),
        right = math.random(1,10),
        answers = {rnd(100),rnd(100),rnd(100),rnd(100)},
        answerIndex = rnd(4)-稍后将覆盖answer [answerIndex]
    }
    obj.answer = obj.left * obj.right
    obj.answers [obj.answerIndex] = obj.answer
    M.times [i] = obj
结束

有什么想法如何解决这个问题? 谢谢。

点赞
用户869951
用户869951

第93行有 "questions[currentQuestion].question":questions表中的每个项都是一个带有左、右等字段的表,但没有字段 "question",你在第93行访问了它。在定义问题的循环中,在 "obj.answer =" 之前添加一行:

 obj.question = string.format("%s x %s", left, right)
2014-02-23 01:00:10