I’m trying to get my high score displayed at the game over screen, but it shows nil every time. On both the game screen and game over screen, it shows "High Score: nil " And this is after a few rounds of gameplay, it doesn’t look like it’s saving. Can someone find what I’m doing wrong? All the scoring scripts…
-- GAME SCREEN --
local score = 0
local scoreText
local highScore
local highScoreText
function loadHighScore()
local highScoreFilename = "highScore.data"
local loadedHighScore = loadValue( highScoreFilename )
highScore = tonumber(loadedHighScore)
end
local scoreText = display.newText("0", 0, 0, native.systemFont, 24)
--scoreText.x/y values
local highScoreText = display.newText("High Score: " .. tostring(highScore, 0, 0, native.systemFont, 24)
--highScoreText.x/y values
-- How the score is added
score = score + 25
scoreText.text = tostring( score )
-- GAME OVER SCREEN --
scoreText.isVisible = false
scoreText.text = "Score: " .. tostring( score )
scoreText.x = 50; scoreText.y = display.contentHeight/2
scoreText:toFront()
timer.performWithDelay( 0, function() scoreText.isVisible = true;end, 1)
highScoreText = display.newText("High Score: " .. tostring( highScore ), 50, scoreText.y + 25, native.systemFontBold, 24)
highScoreText:setTextColor( 255, 0, 0 )
Save/Load values
[code]
– Save Value
local saveValue = function( strFilename, strValue )
– will save specified value to specified file
local theFile = strFilename
local theValue = strValue
local path = system.pathForFile( theFile, system.DocumentsDirectory )
– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “w+” )
if file then
– write game score to the text file
file:write( theValue )
io.close( file )
end
end
– Load Value
local loadValue = function( strFilename )
– will load specified file, or create new file if it doesn’t exist
local theFile = strFilename
local path = system.pathForFile( theFile, system.DocumentsDirectory )
– io.open opens a file at path. returns nil if no file found
local file = io.open( path, “r” )
if file then
– read all contents of file into a string
local contents = file:read( “*a” )
io.close( file )
return contents
else
– create file b/c it doesn’t exist yet
file = io.open( path, “w” )
file:write( “0” )
io.close( file )
return “0”
end
end
[import]uid: 114389 topic_id: 30713 reply_id: 330713[/import]
