是否需要在 corona build.settings 中设置一些特定权限才能将高分永久保存在文件中?
每次运行代码时都会出现“权限被拒绝”的错误
如何纠正这个错误?
这是我尝试过的代码:
function read_score()
local f1 = assert(io.open(path, "r+"))
local contents = f1:read( "*a" )
highScore = tonumber(contents)
if highScore==nil
then highScore=0
elseif score>highScore
then
highScore=score
end
f1:write(highScore)
f1:close()
disp_permScore()
end
function disp_permScore() --Function to display the high score
local f1 = assert(io.open(path, "r"))
local contents = f1:read( "*a" )
highScore = tonumber(contents)
text_display2= display.newText(" BEST: " ..highScore, 0, 0, "Helvetica", 90)
text_display2.x = centerX
text_display2.y = centerY + 80
text_display2.alpha=1
f2:close()
end
function gameOver()
local f1 = assert(io.open(path, "r+"))
local contents = f1:read( "*a" )
highScore = tonumber(contents)
if score<highScore
then
disp_permScore()
else
read_score()
end
请告诉我哪里出错了?
还请说明如何纠正?我是这门语言的新手,这是我第一次尝试构建。
Thanks
EDIT:
function read_score()
local f1 = assert(io.open(path, "r"))
local contents = f1:read( "*a" )
highScore = tonumber(contents)
f1:close()
if highScore==nil
then highScore=0
elseif score>highScore
then
highScore=score
local f2=assert(io.open(path, "w"))
f2:write(highScore)
f2:close()
end
end
function disp_permScore()
local f1 = assert(io.open(path, "r"))
local contents = f1:read( "*a" )
highScore = tonumber(contents)
text_display2= display.newText("GAME OVER!\n BEST: " ..highScore, 0, 0, "native.systemFontBold", 80)
text_display2.x = centerX
text_display2.y = centerY
text_display2.alpha=1
f1:close()
end
function gameOver()
mainScreen()
disp_permScore()
现在请查看上面编辑后的代码。现在,当我使用旧文件运行此代码(之前已打开,它运行良好,然后永久保存代码)..但是当我尝试打开新文件时,代码失败。 (我认为那是因为我调用了 read_score() 和 disp_permScore() 函数,它们最初以“读”模式打开文件 - 引发了错误)但是,如何纠正这个错误?
P.S:当我将“r”模式更改为“r+”模式时,同样的错误再次出现。
请帮忙
EDIT 2:
function saveScore()
local path = system.pathForFile("scoredata001.txt", system.DocumentsDirectory)
local file = io.open(path, "w")
if file then
local score=get_score() --The get_score() returns the value of current score which is saved in 'score'.
local newScore = compareScore()
local contents = tostring( newScore )
file:write( contents )
io.close( file )
return true
else
print("Error: could not write Score")
return false
end
end
function loadScore()
local path = system.pathForFile("scoredata001.txt", system.DocumentsDirectory)
local contents = ""
local file = io.open( path, "r" )
if file then
local contents = file:read( "*a" )
local score = tonumber(contents);
io.close( file )
return score
end
print("Could not read scores from scoredata.txt")
return nil
end
function return_highScore()
local highscore=loadScore()
if highscore==nil
then highscore=0
end
return highscore
end
function compareScore()
local highscore=return_highScore()
if highscore
then
local currscore=get_score()
if highscore==0
then
return highscore
elseif currscore>highscore
then
return currscore
end
end
return true
end
function disp_permScore()
local display_score=return_highScore()
text_display2= display.newText("GAME OVER!\n BEST: " ..display_score, 0, 0, "Helvetica", 80)
text_display2.x = centerX
text_display2.y = centerY
text_display2.alpha=1
function gameOver()
mainScreen()
saveScore()
disp_permScore()
end
请看一下这个?
由于我想同时显示高分和当前分数,所以我修改了之前的分数。当前分数显示完美。这是我昨天晚上尝试过的。
但现在,高分没有保存在文件中。 (即最好的始终显示 0)
另外,cmd 说“无法从 Scoredata.txt 读取分数”
我无法找到我哪里出错了。
请帮忙解决这个问题?