本文介绍了在lua的权限问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我需要在corona build.settings中设置一些特定的权限来永久保存文件中的高分吗?代码说Permission denied
如何纠正这个错误?

这里是我试过的代码:

            ( f1:write(highScore))的参数指定的r +)) 。 

您需要打开文件,读取上下文,然后关闭它,然后重新打开文件()并写入内容。



更新后的代码失败,因为你正在以读模式打开不存在的文件,你应该得到 nil 作为 open()的结果。 调用和没有这样的文件或目录或类似的东西返回第二个值。您需要替换断言检查的结果打开命令和if打开失败,然后检查错误,看是否因为文件是新的而失败。



看到精确错误会非常有帮助你会得到下一次(以及任何线路信息和堆栈跟踪,如果可用)。

Is it required to set some specific permissions in corona build.settings to save high score in file permanently?

I'm getting and error every time I run the code saying "Permission denied" How to rectify this error?

Here is the code I tried:

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

Please tell where do I go wrong?Also please explain how to rectify it? I'm new to this language and this is my first ever build I'm trying.

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()

Please heave a look above at the edited code now. Now whenI run this code by using an old file( which had been opened earlier, it runs good then saving the code permanently).. but when I try opening a new file, the code fails. (I think thats because I call the read_score() and disp_permScore() functions which initially opens file in 'read' mode - is raising the error) But then, how to rectify this error?P.S : when I change "r" mode to "r+" mode, the same error rises again.Please help

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

Please have a look at this one?Since I wanted to display both high score and current score, I modified the previous score.. Current score is being displayed perfect. This is something I tried yesterday night.But now, the high score is not being saved in file. (i.e. the best always displays 0)Also, the cmd says "unable to read scores form scoredata.txt)I'm unable to find where I went wrong.Please help with this?

解决方案

You are opening file for reading (local f1 = assert(io.open(path, "r+"))) as specified by "r" parameter, but later are trying to write to it (f1:write(highScore)).

You need to open file, read the context, and close it; then reopen it for writing (using "w" mode) and write the content.

The updated code fails because you are opening in read mode the file that doesn't exist. You should get nil as the result of open() call and "No such file or directory" or something similar as the second value returned. You need to replace assert with a check on the result of the open command and if open fails, then check the error to see if it fails because the file is new.

It would be quite helpful to see the exact error you are getting next time (along with any line information and the stack trace if available).

这篇关于在lua的权限问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:01