我有这个错误:
尝试连接全局“高分”(一个
nil
值)
这就是我在 Game.lua 文件中检查高分的方式:
function HighscoreUpdate()
if(score>Highscore)then
Highscore = score
end
end
这就是我将 Highscore 保存在 score.txt 中的方式,并在所有 CollisionChecks 之后调用它(我在这里没有任何错误):
function savescore(hs)
local path = system.pathForFile( "score.txt", system.DocumentsDirectory )
local file = io.open ( path, "w" )
local contents = tostring(hs)
file:write( contents )
file:close( )
end
这就是我尝试加载它的方式(我认为这是问题所在):
loadScores = function()
local scores = {}
local str = ""
local n = 1
local path = system.pathForFile( "score.txt", system.DocumentsDirectory )
local file = io.open ( path, "r" )
if file == nil then
return 0
end
local contents = file:read( "*a" )
file:close()
for i = 1, string.len( contents ) do
local char = string.char( string.byte( contents, i ) )
if char ~= "|" then
str = str..char
else
scores[n] = tonumber( str )
n = n + 1
str = ""
end
end
return scores[1]
end
有任何想法吗?