问题
I have nearly finished my game (just graphics and this to do) but do not know how I can save high scores. I searched corona's apis but did not manage to find what I needed. I also downloaded this http://techority.com/2011/12/28/ego-easy-saving-and-loading-in-your-corona-apps/ but it kept saving the wrong high scores...
Any ideas on the best and simplest way to do this will be greatly appreciated,
thanks in advance.
回答1:
I use the following to save highscores attached to my game. It is not EXACTLY the solution you want but you should be able to modify this for your needs.
I declare a global variable called highscore that keeps track of all the scores. Since I have a level selection screen and highscore screen I have decided to declare these in my menu so I can have them loaded and available before navigating the game.
I can then get each individual highscore for each level by simply calling Highscores[levelNumber]
. When I want to change the highscore I simply call Highscores[1] = 500
and remember to call saveHighscores()
Put the following into your main
local highscoreHandler = require("highscoreHandler")
highscores =
{
0, 0, 0, 0, 0
}
highscores = loadHighscores()
And put this into a seperate file called highscoreHandler.lua
local json = require "json"
function loadHighscores()
local base = system.pathForFile( "highscores.json", system.DocumentsDirectory)
local jsoncontents = ""
local highscoresArray = {}
local file = io.open( base, "r" )
if file then
local jsoncontents = file:read( "*a" )
highscoresArray = json.decode(jsoncontents);
io.close( file )
return highscoresArray
end
return highscores
end
function saveHighscores()
local base = system.pathForFile( "highscores.json", system.DocumentsDirectory)
local file = io.open(base, "w")
local jsoncontents = json.encode(highscores)
file:write( jsoncontents )
io.close( file )
end
来源:https://stackoverflow.com/questions/24810358/simple-way-to-keep-track-of-a-high-score-with-corona-sdk