是否需要在corona build.settings中设置一些特定权限才能永久保存文件中的高分?
每次运行代码“Permission denied”时,我都会收到错误 如何纠正这个错误?
这是我尝试过的代码:
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
请告诉我哪里出错了? 还请解释一下如何纠正它?我是这种语言的新手,这是我正在尝试的第一次构建。
由于
编辑:
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 +”模式时,同样的错误再次上升。 请帮忙
编辑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中的分数” 我无法找到出错的地方。 请帮帮忙吗?
答案 0 :(得分:3)
您正在打开文件以供阅读(local f1 = assert(io.open(path, "r+"))
),由&#34; r&#34;参数,但后来试图写入它(f1:write(highScore)
)。
您需要打开文件,阅读上下文并关闭它;然后重新打开它进行写作(using "w" mode)并写下内容。
更新的代码失败,因为您在读取模式下打开了不存在的文件。作为nil
调用的结果,您应该获得open()
并且&#34;没有这样的文件或目录&#34;或类似于返回的第二个值。您需要替换assert
并检查open命令的结果,如果打开失败,则检查错误以查看它是否因为文件是新的而失败。
看到下次出现的完全错误(以及任何行信息和堆栈跟踪,如果有的话)将会非常有用。