我有這個簡單的遊戲,由此產生數學問題,但效率非常低,因爲我必須自己手動完成所有問題。我如何隨機用Corona SDK爲Lua生成數字
有沒有人知道一些比這更好的代碼或指導我如何設置這個好教程?謝謝。
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
更新:
{
a = math.random(1, 20),
b = math.random(1, 20),
question = a * b,
answer = math.random(m, n)
}
我想這會工作,但我在控制檯收到此錯誤:
mathQuestions.lua:55: attempt to perform arithmetic on global 'a' (a nil value)
更新#2
--mathQuestions.lua
M["times"] = {
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
}
我得到這個錯誤:
ERROR: Failed to execute new (params) function on 'game'
mathQuestions.lua:121: unexpected symbol near 'local'
有誰知道一些比這更好的代碼或指導我如何設置這個好教程? – crentist
請在121後標出行,並在你的更新中標出哪一個是121. – Schollii
@Schollii'local rnd = function(x)return math.random(1,x)end' – crentist