I am working on a game that saves the highscore and the amount of coins. This is all done in a single function. When running on the simulator, the game correctly makes a new file if there is not one already there, and saves the data. On the device, however, the game does not do it at all and makes the game freeze.
it would be nice if you would look at my code:
none of the variables are nil;
-local base = base or system.ResourceDirectory
function highscorefunction()
local filePath = system.pathForFile( “highscoresave.txt” , system.DocumentsDirectory)
local exists = false
– file may exist. won’t know until you open it
local fileHandle = io.open( filePath, “r” )
if (fileHandle) then – nil if no file found
io.close(fileHandle)
file = io.open(filePath,“r”)
if scorenumber > file:read("*n") then
io.close(file)
file2 = io.open (filePath,“w”)
file2:write (scorenumber)
io.close(file2)
end
–do a bunch of stuff like make set the highscore
else
fileHandle2 = io.open (filePath, “w”)
fileHandle2:write(scorenumber)
– lhs = display.newText(scorenumber,240,410)
its0 = 1
io.close(fileHandle2)
end
fileR = io.open(filePath,“r”)
data = fileR:read("*a")
print (data)
lhs = display.newText(data,140,320,“Bold”,30)
io.close (fileR)
local Filepath2 = system.pathForFile( “starnum.txt” , system.DocumentsDirectory)
local SfileHandle = io.open( Filepath2, “r” )
if (SfileHandle) then – nil if no file found
– remve this, it is only for testing
io.close(SfileHandle)
Sfile = io.open(Filepath2,“r”)
beginningamount = Sfile:read("*n")
io.close(Sfile)
Sfile2 = io.open (Filepath2,“w”)
starnumberforshowing = ssscore + beginningamount
Sfile2:write (ssscore + beginningamount)
print(ssscore + beginningamount)
io.close(Sfile2)
its0 = 1;
–do a bunch of stuff like make set the highscore
else
SfileHandle = io.open (Filepath2, “w”)
SfileHandle:write(ssscore)
starnumberforshowing = ssscore
io.close(SfileHandle)
end
starcount = display.newText("Coins: "…starnumberforshowing,150,480,“Bold”,30)
starcount:setReferencePoint(display.CenterReferencePoint)
starcount.x = 150 ; starcount.y = 480;
end