I have two scenes: the game.lua file and a restart.lua file. Once the game.lua file ends it transfers to the restart screen. On the restart screen I have 'your current score:' and 'your highscore:' along with the values. However, the values don't update themselves after each subsequent restart. The highscore won't update until I restart the app and the current score won't reset to zero until I restart the app.
So for example: i)Say my current highscore is 21. I play the game once and achieve a new highscore: 23. My current score goes to 23 but my highscore is still 21 (until I restart the app).
ii)I play again(without restarting the app) and get a score of 5. The restart screen still shows 23 as my current score.
So basically once I play the game once, all scores are stuck.
In the app I am using the ego module to save highscore(as that would have to be permanent) and have my current score set as global.
Here is the code in my game scene:
highscore = loadFile("score.txt")--the ego module loads this file for me
score = 0--kept global to use in restart screen
local scoreText = display.newText(score,W,0)--create text
sceneGroup:insert(scoreText)
local function checkForFile()
if highscore == "empty" then
highscore = 0--gives highscore number if file is empty
saveFile("score.txt", highscore)--saves new highscore
end
end
checkForFile()
local function onObjectTouch(event)
if(event.phase == "began") then
score = score+1--increment score on touch
scoreText.text = score
if score>tonumber(highscore) then
saveFile("score.txt",score)--if new highscore, save score as highscore
end
vertical = -150--set ball's velocity
ball:setLinearVelocity(0,vertical)
print(ball.x .. " " .. event.x)
end
end
Runtime:addEventListener("touch", onObjectTouch)
And here is the code in my restart scene
------------highScore Text----------------
myscore = loadFile("score.txt")--load highscore file
local highscoretext = display.newText( "Your high score:"..myscore, 0, 0, native.systemFontBold, 20 )
highscoretext:setFillColor(1.00, .502, .165)
--center the title
highscoretext.x, highscoretext.y = display.contentWidth/2, 200
--insert into scenegroup
sceneGroup:insert( highscoretext )
--------------yourscore----------------
local yourscoretext = display.newText( "Your score:"..score, 0, 0, native.systemFontBold, 20 )
yourscoretext:setFillColor(1.00, .502, .165)
--center the title
yourscoretext.x, yourscoretext.y = display.contentWidth/2, 230
--insert into scenegroup
sceneGroup:insert( yourscoretext )