Требуется ли установить определенные разрешения в 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
Пожалуйста, скажите, где я ошибся? Также объясните, как исправить это? Я новичок в этом языке, и это мой первый сборник, который я пытаюсь сделать.
Благодаря
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()
Пожалуйста, бросать взгляд выше в редактируемом коде сейчас. Теперь, когда я запускаю этот код, используя старый файл (который был открыт ранее, он работает хорошо, а затем сохраняет код навсегда). Но когда я пытаюсь открыть новый файл, код не работает. (Я думаю, это потому, что я вызываю функции read_score() и disp_permScore(), которые изначально открывают файл в режиме «чтения» - вызывает ошибку). Но тогда, как исправить эту ошибку? P.S: когда я меняю режим «r» на «r +», эта же ошибка снова возрастает. Пожалуйста, помогите
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
Пожалуйста, посмотрите на этот? Поскольку я хотел отображать как высокий балл, так и текущий балл, я изменил предыдущий балл. Текущая оценка отображается отлично. Это то, что я пробовал вчера вечером. Но теперь высокий балл не сохраняется в файле. (Т.е. лучше всегда отображает 0) Кроме того, CMD говорит «не удалось прочитать оценки сформировать scoredata.txt) Я не могу найти, где я не заладилось. Пожалуйста, помогите с этим?
Ошибка, похоже, исходит от 'assert'. 'write' не вызывает ошибок. – lhf
Пожалуйста, взгляните на отредактированный код. Я объяснил, какая ошибка возникает после редактирования. Пожалуйста, расскажите, где я ошибаюсь и как его исправить. – Ozitrick
Пожалуйста, взгляните на EDIT: 2 Я пробовал это вчера вечером. Скажите, где я могу пойти не так? Также, если возможно, укажите (или отредактируйте) правильный код, пожалуйста? – Ozitrick