【问题标题】:Lua (Corona SDK) Android issue - interval is emptyLua(Corona SDK)Android问题-间隔为空
【发布时间】: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"}

标签: android lua coronasdk


【解决方案1】:

错误消息bad argument #1 to 'random' (interval is empty) 表示您正在尝试将零传递给math.random()
换句话说,你的wordsList 数组是空的。

【讨论】:

  • 知道为什么这只会在 PC 上出现问题吗?难道是在Android上它无法读取文本文件或无法以某种方式访问​​它?
  • 可能在你的安卓系统上words.txt文件为空或不正确或与PC上的不一样。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-04
  • 1970-01-01
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多