How Do I Update More Often?

I have highscores working perfectly, but one problem, it only updates when the app is reloaded, and if installed on device I must kill the process and reopen app, otherwise, even though the highscore is saved, it doesn’t display it.

Download the a.zip (project files) You’ll see that the left number is highscore, but only updates upon relaunch/corona restart.

PLLLLLEEEASE HELP :wink:

–Hide the status bar

display.setStatusBar(display.HiddenStatusBar)

–Saving/Loading Stuff

local ego = require “ego”

local saveFile = ego.saveFile

local loadFile = ego.loadFile



–Create the background and make it blue

local bg = display.newRect( 0, 0, 320, 480 )

bg:setFillColor( 150, 180, 200 )

–Start the score at 0

local score = 0

–Create score text and make it dark gray

local scoreText = display.newText(score, 200, 20, native.systemFont, 24)

scoreText:setTextColor( 80, 80, 80 )

–Function to add to score and update scoreText

local function addToScore()

score = score + 1

scoreText.text = score

end

bg:addEventListener(“tap”, addToScore)



–Load highscore value from file. (It will initally be a string.)

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()

–Print the current highscore

local hs = display.newText(highscore, 100, 20, native.systemFont, 24)

hs:setTextColor( 80, 80, 80 )



–When the app is quit (or simulator refreshed) save the new highscore

–(If score > highscore the data will not be changed)

local function onSystemEvent ()

if score > tonumber(highscore) then --We use tonumber as highscore is a string when loaded

saveFile(“highscore.txt”, score)

end

end

Runtime:addEventListener( “system”, onSystemEvent )

  1. Your code is for pre graphics 2.0, so for most users it won’t even run without the compatibility setting.

  2. Nothing in the code you’ve printed here updates the highscore text object (‘hs’)

I would strongly suggest to make a function to update the highscore, and then call it inside of addToScore().

  1. Your code is for pre graphics 2.0, so for most users it won’t even run without the compatibility setting.

  2. Nothing in the code you’ve printed here updates the highscore text object (‘hs’)

I would strongly suggest to make a function to update the highscore, and then call it inside of addToScore().