Необходимо ли е да зададете някои специфични разрешения в corona build.settings, за да запазите високия резултат във файл за постоянно?
Получавам грешка всеки път, когато стартирам кода, който казва „Разрешението е отказано“ Как да поправя тази грешка?
Ето кода, който опитах:
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(), които първоначално отварят файла в режим „четене“ - повдигат грешката) Но тогава как да поправя тази грешка? 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) Не мога да намеря къде сбърках. Моля, помогнете с това?