当前位置: 首页 > 知识库问答 >
问题:

Lua错误:应为字符串,得到nil--参数错误#2

顾乐家
2023-03-14

这是一个Lua脚本,用于Corona SDK中的一个游戏。一开始(旧代码)效率很低,我不得不手动创建每道数学题,在其他人的帮助下(新代码),所以我得到了答案。

在控制台中,我现在看到了这个错误:

第93行:本地问题文本=display.new文本

game.lua:93:错误的参数#2到'newText'(字符串预期,得到零)

--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个答案

祖迪
2023-03-14

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

obj.question = string.format("%s x %s", left, right)
 类似资料: