1
我对updateScore有问题,我的功能是如果用户第一次玩游戏。updateScore无法正常工作
它会创建一个名为myFile.txt的文件来记录分数,现在要做的代码是(如果读者那么)看文件是否存在,如果不存在,它会去我的其他如果已经有一个文件,那么我的内容应该有分数值,然后我可以用它来比较并获得我的高分。
问题是我的内容总是返回nil
价值,因此当你玩的时候总会得到的分数会取代应该是我的高分的分数,我不知道我做错了什么。
这里是我的代码
function updateScore()
local path = system.pathForFile("myfile.txt", system.DocumentsDirectory)
local reader = io.open(path, "r")
local file = io.open(path, "w")
if reader then
reader:close()
local reader1 = io.open(path, "r")
local contents = reader1:read("*n")
if (stopscore == false) then
score = score + 1
scoreText.text = "score: " .. score
scoreText:setReferencePoint(display.CenterLeftReferencePoint)
scoreText.x = 0
scoreText.y = 30
end
if (stopscore == true) then
if (contents == nil) then
local file = io.open(path, "w")
file:write(score)
file:flush()
file:close()
timer.pause(timer1)
director:changeScene("menu", "downFlip")
else
if (contents < score) then
file:write(score)
file:flush()
file:close()
timer.pause(timer1)
director:changeScene("menu", "downFlip")
else
file:write(contents)
file:flush()
file:close()
timer.pause(timer1)
director:changeScene("menu", "downFlip")
end
end
end
else
local file1 = io.open(path, "w")
local walaVal=0
file1:write(walaVal)
file1:close()
if (stopscore == false) then
score = score + 1
scoreText.text = "score: " .. score
scoreText:setReferencePoint(display.CenterLeftReferencePoint)
scoreText.x = 0
scoreText.y = 30
print(contents)
end
if (stopscore == true) then
local file = io.open(path, "w")
file:write(score)
file:flush()
file:close()
timer.pause(timer1)
director:changeScene("menu", "downFlip")
end
end
end
的感谢!它创造奇迹 – user2596861