在lua的权限问题 [英] Permission issues in lua

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

问题描述



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

这里是我试过的代码:

 函数read_score()

本地f1 =断言(io.open(path,r +))
本地内容= f1:读(* a)
highScore = tonumber(内容)
if highScore ==零
then highScore = 0


elseif得分> highScore
然后
highScore =分数
结束
f1:写入(highScore)
f1:结束()
disp_permScore()

结束


函数disp_permScore() - 函数显示高分
本地f1 =断言(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()
结束


函数gameOver()
local f1 = assert(io.open(path,r +))
local contents = f1:read(* a)
highScore = tonumber(contents)

如果得分 then
disp_permScore()
else
read_score()
end

请告诉我哪里出错了?
也请解释如何纠正它?

谢谢

编辑:

pre $函数read_score
$ b $本地f1 =断言(io.open(path, r))
本地内容= f1:read(* a)
highScore = tonumber(contents)
f1:close()
if highScore == nil
然后highScore = 0
elseif得分> highScore
然后
highScore =得分
本地f2 =断言(io.open(path,w))
f2:write(highScore)
f2:close()
结束

$ b结束


函数disp_permScore()
local f1 = assert(io.open(path,r))
local content = 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

$ b函数gameOver()

电源creen()
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
本地分数= get_score() - get_score()返回保存在分数中的当前分数的值。
本地newScore = compareScore()
本地内容= tostring(newScore)
文件:写入(内容)

io.close(文件)
返回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(文件)
返回分数
结束
print(无法从scoredata.txt读取分数)
返回零
结束

函数return_highScore()
本地highscore = loadScore()
如果高分== nil
then highscore = 0
结束
返回高分
end
$ b b函数compareScore()
本地highscore = return_highScore()
如果高分

本地currscore = get_score()
if highscore == 0
then
return highscore
elseif currscore> highscore
then
return currscore
end
end
返回true
结束


函数disp_permScore()
本地display_score = return_highScore()
text_display2 = display.newText(GAME OVER!\\\
BEST:..display_score,0,0,Helvetica,80)
text_display2.x = centerX
text_display2.y = centerY
text_display2.alpha = 1

函数gameOver()

mainScreen()
saveScore()
disp_p ermScore()
end

请看看这个吗?
因为我想显示高分和当前分数,所以我修改了以前的分数。当前分数正在显示完美。这是昨天晚上我试过的东西。
但现在,高分并没有被保存在文件中。 (即最好总是显示0)
此外,CMD说:无法读取形式scoredata.txt分数)
我无法找到我出错的地方
请帮助这个 f1:write(highScore))的参数指定的r +)) 。

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



更新后的代码失败,因为你正在以读模式打开不存在的文件,你应该得到 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的权限问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆