I’m pretty much done with the high score part of my game, but I still have one problem. When the user starts playing the game, their high score is 0. The way I have my code set up, each time the current score is greater than the high score, the high score is set to the current score. The problem is, during one UNCLOSED game play session, corona saves the high score each time it is greater than the original high score, which is 0. I want the high score to reset each time a new high score is achieved. I have attached a couple of files, if anyone could help, that would be awesome. Thanks. I can only do one file so the next one will be in a comment.
module (…, package.seeall)
–Save function
function saveFile( fileName, fileData )
--Path for file
local path = system.pathForFile( fileName, system.DocumentsDirectory )
--Open the file
local file = io.open( path, “w+” )
--Save specified value to the file
if file then
file:write( fileData )
io.close( file )
end
end
–Load function
function loadFile( fileName )
–Path for file
local path = system.pathForFile( fileName, system.DocumentsDirectory )
–Open the file
local file = io.open( path, “r” )
--If the file exists return the data
if file then
local fileData = file:read( “*a” )
io.close( file )
return fileData
--If the file doesn’t exist create it and write it with “empty”
else
file = io.open( path, “w” )
file:write( “empty” )
io.close( file )
return “empty”
end
end
In case the main file didn’t attach, it is:
local ego = require “ego”
local saveFile = ego.saveFile
local loadFile = ego.loadFile
local physics = require( “physics” )
physics.start()
–hiding the status bar and starting the physics engine
display.setStatusBar( display.HiddenStatusBar )
local physics = require (“physics”)
physics.start(true)
highscore = loadFile (“highscore.txt”)
–If the file is empty (this means it is the first time you’ve run the app) save it as 0
local function checkForFile ()
if highscore == “empty” then
highscore = 0
saveFile(“highscore.txt”, highscore)
end
end
checkForFile()
hsstuff = display.newText( highscore, display.contentWidth/2, display.contentHeight/2+215, “MANIFESTO”, 50)
hsstuff:setFillColor( 0,1,1 )
–Function to manage body state after slight delay
local function handleCollisionOnDelay( event )
if currentScore > tonumber(highscore) then --We use tonumber as highscore is a string when loaded
saveFile(“highscore.txt”, currentScore)
loadFile(“highscore.txt”, currentScore)
newhs = display.newText( “High!:” … currentScore, display.contentWidth/2, display.contentHeight/2+180,“MANIFESTO”, 40)
newhs:setFillColor( 0,1,1 )
local function vanish()
newhs.isVisible = false
end
timer.performWithDelay( 3000, vanish)
end
currentScore = 0
local obj = event.source.object
--Change the body’s active state to false
crate.isBodyActive = false
obj.isBodyActive = false
obj.x = display.contentWidth/2
obj.y = display.contentHeight/2
obj.rotation = 0
timer.pause( scoreTimer )
end
currentScore = 0
local scoreTxt = display.newText( currentScore , display.contentWidth/2+100, display.contentHeight/2-230, “MANIFESTO”, 65)
scoreTxt:setFillColor( 0, 1, 1 )
local function scoreKeeper( event )
currentScore = currentScore + 1000
scoreTxt.text = currentScore
end
scoreTimer = timer.performWithDelay( 1000, scoreKeeper, -1 )
timer.pause( scoreTimer )
function crate:touch( event )
if event.phase == “began” then
timer.resume( scoreTimer )
currentScore = 0
end
end
crate:addEventListener( “touch”, crate)
I see:
loadFile(“highscore.txt”, currentScore)
But the function is only expecting one option, the file name. Try changing that to:
currentScore = loadFile(“highscore.txt”)
I would just stick a bunch of print statements in the code to try an narrow down exactly where the problem is.
Will that mess up the rest of the game, though, because I already have currents ore set as the value for a timer?
I don’t see where you ever refresh the highscore. That line should have been:
highscore = loadFile(“highscore.txt”)
I know using print statements are an old fashioned way of debugging, but if you throw in a few to print currentScore and highscore it would give you a better clue as to why the code isn’t working.
The problem is fixed. Peach Pellen helped me. I’ll post the revised code later.
module (…, package.seeall)
–Save function
function saveFile( fileName, fileData )
--Path for file
local path = system.pathForFile( fileName, system.DocumentsDirectory )
--Open the file
local file = io.open( path, “w+” )
--Save specified value to the file
if file then
file:write( fileData )
io.close( file )
end
end
–Load function
function loadFile( fileName )
–Path for file
local path = system.pathForFile( fileName, system.DocumentsDirectory )
–Open the file
local file = io.open( path, “r” )
--If the file exists return the data
if file then
local fileData = file:read( “*a” )
io.close( file )
return fileData
--If the file doesn’t exist create it and write it with “empty”
else
file = io.open( path, “w” )
file:write( “empty” )
io.close( file )
return “empty”
end
end
In case the main file didn’t attach, it is:
local ego = require “ego”
local saveFile = ego.saveFile
local loadFile = ego.loadFile
local physics = require( “physics” )
physics.start()
–hiding the status bar and starting the physics engine
display.setStatusBar( display.HiddenStatusBar )
local physics = require (“physics”)
physics.start(true)
highscore = loadFile (“highscore.txt”)
–If the file is empty (this means it is the first time you’ve run the app) save it as 0
local function checkForFile ()
if highscore == “empty” then
highscore = 0
saveFile(“highscore.txt”, highscore)
end
end
checkForFile()
hsstuff = display.newText( highscore, display.contentWidth/2, display.contentHeight/2+215, “MANIFESTO”, 50)
hsstuff:setFillColor( 0,1,1 )
–Function to manage body state after slight delay
local function handleCollisionOnDelay( event )
if currentScore > tonumber(highscore) then --We use tonumber as highscore is a string when loaded
saveFile(“highscore.txt”, currentScore)
loadFile(“highscore.txt”, currentScore)
newhs = display.newText( “High!:” … currentScore, display.contentWidth/2, display.contentHeight/2+180,“MANIFESTO”, 40)
newhs:setFillColor( 0,1,1 )
local function vanish()
newhs.isVisible = false
end
timer.performWithDelay( 3000, vanish)
end
currentScore = 0
local obj = event.source.object
--Change the body’s active state to false
crate.isBodyActive = false
obj.isBodyActive = false
obj.x = display.contentWidth/2
obj.y = display.contentHeight/2
obj.rotation = 0
timer.pause( scoreTimer )
end
currentScore = 0
local scoreTxt = display.newText( currentScore , display.contentWidth/2+100, display.contentHeight/2-230, “MANIFESTO”, 65)
scoreTxt:setFillColor( 0, 1, 1 )
local function scoreKeeper( event )
currentScore = currentScore + 1000
scoreTxt.text = currentScore
end
scoreTimer = timer.performWithDelay( 1000, scoreKeeper, -1 )
timer.pause( scoreTimer )
function crate:touch( event )
if event.phase == “began” then
timer.resume( scoreTimer )
currentScore = 0
end
end
crate:addEventListener( “touch”, crate)
I see:
loadFile(“highscore.txt”, currentScore)
But the function is only expecting one option, the file name. Try changing that to:
currentScore = loadFile(“highscore.txt”)
I would just stick a bunch of print statements in the code to try an narrow down exactly where the problem is.
Will that mess up the rest of the game, though, because I already have currents ore set as the value for a timer?
I don’t see where you ever refresh the highscore. That line should have been:
highscore = loadFile(“highscore.txt”)
I know using print statements are an old fashioned way of debugging, but if you throw in a few to print currentScore and highscore it would give you a better clue as to why the code isn’t working.
The problem is fixed. Peach Pellen helped me. I’ll post the revised code later.