2014-10-30 29 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