2014-10-30 124 views
2

是否需要在corona build.settings中设置一些特定的权限以永久保存文件中的高分?lua中的权限问题

我收到并且每次运行代码时都会显示“允许被拒绝”的错误 如何纠正这个错误?

这里是我试过的代码:

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()函数,最初以'read'模式打开文件 - 正在引发错误)但是,如何纠正这个错误? 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) 我无法找到我哪里错了。 请帮助呢?

回答

3

您正在打开文件对于由“R”参数中指定阅读(local f1 = assert(io.open(path, "r+"))),但后来试图写它(f1:write(highScore)

您需要打开文件,读取范围内,并关闭它,然后重新打开写入(using "w" mode )并写入内容

更新的代码失败,因为您在读模式下打开不存在的文件。作为open()调用和“没有这样的文件或目录”或类似于返回的第二个值的结果,您应得到nil。您需要替换assert并检查open命令的结果,如果打开失败,则检查错误以查看是否因为文件是新的而失败。

看到您下一次得到的确切错误(以及任何行信息和堆栈跟踪(如果可用))将是相当有用的。

+0

错误似乎来自'assert'。 'write'不会引发错误。 – lhf 2014-10-30 23:43:19

+0

请看看编辑好的代码。我已经解释了编辑后出现的错误。请告诉我哪里出了问题,以及如何纠正它。 – Ozitrick 2014-10-31 01:03:57

+0

请看看编辑:2 昨天晚上我试了这个。请告诉我哪里出错了? 另外,如果可能,请提供(或编辑)正确的代码吗? – Ozitrick 2014-11-01 08:20:28