【发布时间】:2015-06-25 21:43:09
【问题描述】:
在 PC 上,我的游戏可以正常编译并正常运行,但是一旦我为 Android 构建它,我就会收到这个奇怪的错误代码:
bad argument #1 to 'random' (interval is empty)
此错误代码来自的代码行是:
local word = wordsList[math.random(#wordsList)]
整个代码段:
local lineCount = 1
local wordsList = {}
local wordAccepted = true
local file = io.open( system.pathForFile( "words.txt", system.ResourceDirectory ), "r" ) --Open the words file from the resource folder
for line in file:lines() do
if #line > 1 and #line <= 10 then
for i = 1,#line do
if string.byte(line,i)<65 or string.byte(line,i)>90 and string.byte(line,i)<97 or string.byte(line,i)>122 then
wordAccepted = false
end
end
if wordAccepted == true then
print ("accepted "..line)
wordsList[lineCount]=string.upper(line)
lineCount = lineCount + 1
else
print("rejected "..line)
end
end
end
io.close( file )
file = nil
local word = wordsList[math.random(#wordsList)]
【问题讨论】:
-
什么是
wordsList?有多大? -
我尝试了大小为 3 和 78 的 wordsList。两者都有同样的问题
-
提供
wordsList的小例子。 -
wordsList 只是从资源文件夹中的“words.txt”文件中取出的单词数组。当我尝试使用 3 个单词(A、B、C)的 wordsList 时,问题仍然存在。我只是想从 wordsList 中选择一个随机单词并将其设置为一个新单词变量。
-
不要只描述代码,要展示它。喜欢
local wordsList = {"A", "B", "C"}。