Lua错误:字符串预期,得到零 - 错误的参数#2

时间:2014-02-22 22:04:00

标签: lua corona lua-table

它是Lua中用于Corona SDK游戏的脚本。起初(旧代码)是非常低效的,我不得不手动创建每个数学问题,使用(新代码)在某人的帮助下,我得到了。

在控制台中,我现在收到此错误:

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 (Old code)
local M = {}
M["times"] = {
    {
        question="6 x 5",  --The question.
        answers={"30", "11", "29", "20"},  --Array of possible answers.
        answer=1   --Which one from the above array is the correct answer.
    },
}
return M

--mathQuestions.lua (New code)
local rnd = function (x) return math.random(1,x) end
M.times = {}
local numQuestions = 10 -- how many questions in your database
for i=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) -- will override answer[answerIndex] later
    }
    obj.answer = obj.left * obj.right
    obj.answers[obj.answerIndex] = obj.answer
    M.times[i] = obj
end

关于问题是什么以及如何解决问题的任何想法?感谢。

1 个答案:

答案 0 :(得分:1)

第93行有“问题[currentQuestion]。问题”:问题表中的每个项目都是一个带有“左”,“右”等字段的表,但没有您在第93行访问的字段“问题”。在您的循环中您定义问题的位置在“obj.answer =”之前添加一行:

obj.question = string.format("%s x %s", left, right)