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
(這是refernce以前的問題Permission issues in lua)與盧阿節省高分問題
基本上我試圖建立在Lua遊戲(這是我的第一次比賽) 但是,我無法將高分保存到文件。如果它被保存,那麼我無法檢索它們。 (總之,我在執行代碼時總會出現一些或其他錯誤/問題
請看看上面的代碼我想同時顯示高分和當前分數當前分數正在顯示完美。是我昨天晚上試過的東西,但是現在,高分並沒有被保存在文件中(也就是說,最好總是顯示0)另外,cmd表示「無法從scoredata.txt讀取分數)我無法找到哪裏我去錯了。
請幫助呢? 請告訴我在哪裏去了? 另外,如果可能的話,提供(或編輯)正確的代碼嗎?
+1關於縮進。因爲它的代碼甚至沒有語法上的有效性(末尾有一個「末端」丟失)。適當的縮進會使這一點變得明顯。對於嘗試閱讀和理解代碼的人來說,這也是一個禮節性的問題... – siffiejoe 2014-11-01 10:51:26