How can I save the current high score in “spstart” and display it in the “spgameover” screen?
spstart:
[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
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
storyboard.gotoScene( “spgameover” )
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, scoreText2
storyboard.removeScene( “spstart” )
local function retry( self, event )
storyboard.gotoScene( “spstart” )
return true
end
local function menu( self, event )
storyboard.gotoScene( “menu” )
return true
end
function scene:createScene( event )
local screenGroup = self.view
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 )
scoreText2 = display.newText( “Score:”, 0, 0, native.systemFont, 30 )
scoreText2.x = display.contentCenterX
scoreText2.y = display.contentCenterY
scoreText2:setTextColor( 255, 0, 0 )
screenGroup:insert( scoreText2 )
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: 34405 reply_id: 334405[/import]