Save the high score when leaving game

I’ve set up a score system and I know how to set up a high score system with If score>high score then new high score=score. But how can I save the high score when I leave the game and come back?

spstart.lua:

[code]local storyboard = require “storyboard”
local scene = storyboard.newScene()
local widget = require “widget”
display.setStatusBar( display.HiddenStatusBar )

local background, ball, score, pauseBtn, resumeBtn, leftWall, rightWall, ceiling, floorr, bottom, lock, lock2, scoreText, scoreText2

storyboard.removeScene( “spgameover” )
storyboard.removeScene( “mpstart” )
storyboard.removeScene( “tbmstart” )
storyboard.removeScene( “pmstart” )
storyboard.removeScene( “main” )
storyboard.removeScene( “menu” )
storyboard.removeScene( “sp” )
storyboard.removeScene( “mp” )
storyboard.removeScene( “tbm” )
storyboard.removeScene( “pm” )
storyboard.removeScene( “s” )
storyboard.removeScene( “mpgameover” )
storyboard.removeScene( “tbmgameover” )
storyboard.removeScene( “pmgameover” )

local physics = require( “physics” )
physics.start()
physics.setGravity( 0, 49 )

–physics.setDrawMode(“hybrid”)

local function onLocalCollision( self, event )
if event.other.myName == “floor” then
if event.phase == “began” then
local options = { effect = “crossFade”, params={ score = score }}
storyboard.gotoScene( “spgameover”, options )
return true
end
end
end

score = 0

local function addScore( event )
if event.phase == “began” then
score = score + 1
scoreText.text = (score … " ")
end
end

local function removeBottom( event )
if event.phase == “began” then
display.remove( bottom )
end
end

function scene:createScene( event )
local screenGroup = self.view

background = display.newImage ( “road.jpg” )
screenGroup:insert( background )

scoreText = display.newText(score … " ", 0, 0, native.systemFont, 20 )
scoreText.x = 305
scoreText.y = 15
scoreText:setTextColor( 255, 0, 0 )
screenGroup:insert( scoreText )

scoreText2 = display.newText( “Score:”, 0, 0, native.systemFont, 20 )
scoreText2.x = 260
scoreText2.y = 15
scoreText2:setTextColor( 255, 0, 0 )
screenGroup:insert( scoreText2 )

ball = display.newImageRect( “SkySoccer Ball.gif”, 100, 100 )
ball.x = display.contentCenterX
ball.y = 429
ball.myName = “ball”
screenGroup:insert( ball )

pauseBtn = display.newImageRect ( “pausebtn.gif”, 50, 50 )
pauseBtn.x = 20
pauseBtn.y = 20
screenGroup:insert( pauseBtn )

resumeBtn = display.newImageRect ( “resumebtn.gif”, 50, 50 )
resumeBtn.x = 20
resumeBtn.y = 20
resumeBtn.isVisible = false
screenGroup:insert( resumeBtn )

leftWall = display.newRect( -10, 0, 10, display.contentHeight )
leftWall.isVisible = false
screenGroup:insert( leftWall )
rightWall = display.newRect( display.contentWidth, 0, 10, display.contentHeight )
rightWall.isVisible = false
screenGroup:insert( rightWall )
ceiling = display.newRect( 0, -10, display.contentWidth, 10 )
ceiling.isVisible = false
screenGroup:insert( ceiling )
floorr = display.newRect( 0, 479, display.contentWidth, 5 )
floorr.myName = “floor”
floorr.isVisible = false
screenGroup:insert( floorr )
bottom = display.newRect( 0, 466, display.contentWidth, 5 )
bottom.isVisible = false
screenGroup:insert( bottom )

physics.addBody( leftWall, “static”, { bounce = 0.3 } )
physics.addBody( rightWall, “static”, { bounce = 0.3 } )
physics.addBody( ceiling, “static”, { bounce = 0.3 } )

physics.addBody( ball, { bounce = 0, radius = 40, friction = 1.0 } )

physics.addBody( floorr, “dynamic”, { bounce = 0, friction = 10, density = 1 } )

physics.addBody( bottom, “dynamic”, { bounce = 0, friction = 10, density = 1 } )

lock = physics.newJoint( “weld”, leftWall, floorr, floorr.x, floorr.y )
lock2 = physics.newJoint( “weld”, leftWall, bottom, bottom.x, bottom.y )

ball.isFixedRotation = true

ball:addEventListener( “touch”, moveBall )

pauseBtn:addEventListener( “touch”, pauseGame )

resumeBtn:addEventListener( “touch”, resumeGame )

ball.collision = onLocalCollision
ball:addEventListener( “collision”, ball )

ball:addEventListener( “touch”, addScore)

ball:addEventListener( “touch”, removeBottom)

end

function moveBall( event )
if event.phase == “began” then
local x, y = ball.x-event.x, ball.y-event.y
ball:applyLinearImpulse( x / 30, -2, ball.x, ball.y )
return true
end
end

function pauseGame( event )
–if end of touch event
if(event.phase == “ended”) then
–pause the physics
physics.pause()
–pause the ball movement
ball:removeEventListener( “touch”, moveBall )
–make pause button invisible
pauseBtn.isVisible = false
–make resume button visible
resumeBtn.isVisible = true
– indicates successful touch
return true
end
end

function resumeGame( event )
–if end of touch event
if(event.phase == “ended”) then
–resume physics
physics.start()
– resume the ball movement
ball:addEventListener( “touch”, moveBall )
–make pause button visible
pauseBtn.isVisible = true
–make resume button invisible
resumeBtn.isVisible = false
– indicates successful touch
return true
end
end

scene:addEventListener( “createScene”, scene )

return scene[/code]

spgameover:

[code]local storyboard = require “storyboard”
local scene = storyboard.newScene()
local widget = require “widget”
display.setStatusBar( display.HiddenStatusBar )

local background, myText, myButton, myButton2, scoreText, scoreText2, scoreText3, score, params

storyboard.removeScene( “spstart” )
storyboard.removeScene( “mpstart” )
storyboard.removeScene( “tbmstart” )
storyboard.removeScene( “pmstart” )
storyboard.removeScene( “main” )
storyboard.removeScene( “menu” )
storyboard.removeScene( “sp” )
storyboard.removeScene( “mp” )
storyboard.removeScene( “tbm” )
storyboard.removeScene( “pm” )
storyboard.removeScene( “s” )
storyboard.removeScene( “mpgameover” )
storyboard.removeScene( “tbmgameover” )
storyboard.removeScene( “pmgameover” )

local function retry( self, event )
local options = { effect = “crossFade” }
storyboard.gotoScene( “spstart”, options )
return true
end

local function menu( self, event )
local options = { effect = “crossFade” }
storyboard.gotoScene( “menu”, options )
return true
end

function scene:createScene( event )
local screenGroup = self.view

params = event.params
score = params.score

background = display.newImage ( “uitlegbg.jpg” )
screenGroup:insert( background )

myText = display.newText( “GAME OVER”, 0, 0, native.systemFont, 40 )
myText.x = display.contentCenterX
myText.y = display.contentWidth / 10
myText:setTextColor( 255, 0, 0 )
screenGroup:insert( myText )

scoreText = display.newText( "Score: " … score, 0, 0, native.systemFont, 30 )
scoreText.x = display.contentCenterX
scoreText.y = 160
scoreText:setTextColor( 255, 0, 0 )
screenGroup:insert( scoreText )

scoreText2 = display.newText( "High score: " … score, 0, 0, native.systemFont, 30 )
scoreText2.x = display.contentCenterX
scoreText2.y = 220
scoreText2:setTextColor( 255, 0, 0 )
screenGroup:insert( scoreText2 )

scoreText3 = display.newText( "Total score: " … score, 0, 0, native.systemFont, 30 )
scoreText3.x = display.contentCenterX
scoreText3.y = 280
scoreText3:setTextColor( 255, 0, 0 )
screenGroup:insert( scoreText3 )

myButton = widget.newButton{
left = 15,
top = 350,
default = “retry.png”,
over = “retry2.png”,
onRelease = retry
}
screenGroup:insert( myButton )
myButton2 = widget.newButton{
left = 165,
top = 350,
default = “menu.png”,
over = “menu2.png”,
onRelease = menu
}
screenGroup:insert( myButton2 )

end

scene:addEventListener( “createScene”, scene )

return scene[/code] [import]uid: 203192 topic_id: 34460 reply_id: 334460[/import]

Hello,
You’ll need to save the high score value to either a local text file or SQLite database. This file will reside inside the app’s “Documents” directory, which is like a local, internal folder the app can access to read/write data that is persistent (as in, it remains for the entire life of the app, until the user removes the app from the device entirely).

These tutorials should help you get started:
http://www.coronalabs.com/blog/2012/02/14/reading-and-writing-files-in-corona/
http://www.coronalabs.com/blog/2012/04/03/tutorial-database-access-in-corona/

If you’re saving just a small amount of data like high score and a few other things, I recommend the text file approach since it’s easier to deal with than an SQLite database. However, if you intend to eventually store a lot of information… multiple per-level high scores, awards, achievements, etc. then a database is a more logical, extendable method.

Best regards,
Brent
[import]uid: 200026 topic_id: 34460 reply_id: 136999[/import]

To add to Brent’s answer, you referenced “Ice” in another thread. Ice can save those scores for you. I’ve never used Ice before, so I can’t really help you. But of you search through the community code there are several functions there to help you load and save data.

But I agree with Brent that reading those tutorials will help you understand what things like ice and the table save functions are doing.
[import]uid: 199310 topic_id: 34460 reply_id: 137026[/import]

Hello,
You’ll need to save the high score value to either a local text file or SQLite database. This file will reside inside the app’s “Documents” directory, which is like a local, internal folder the app can access to read/write data that is persistent (as in, it remains for the entire life of the app, until the user removes the app from the device entirely).

These tutorials should help you get started:
http://www.coronalabs.com/blog/2012/02/14/reading-and-writing-files-in-corona/
http://www.coronalabs.com/blog/2012/04/03/tutorial-database-access-in-corona/

If you’re saving just a small amount of data like high score and a few other things, I recommend the text file approach since it’s easier to deal with than an SQLite database. However, if you intend to eventually store a lot of information… multiple per-level high scores, awards, achievements, etc. then a database is a more logical, extendable method.

Best regards,
Brent
[import]uid: 200026 topic_id: 34460 reply_id: 136999[/import]

To add to Brent’s answer, you referenced “Ice” in another thread. Ice can save those scores for you. I’ve never used Ice before, so I can’t really help you. But of you search through the community code there are several functions there to help you load and save data.

But I agree with Brent that reading those tutorials will help you understand what things like ice and the table save functions are doing.
[import]uid: 199310 topic_id: 34460 reply_id: 137026[/import]