How to stop game and make option to restart

Here is my first game that im trying to make. Its almost done just need to make option to stop it and then to make it restart not sure how to do that and i added code for restart button in lines 29 to down and changeScene function from line 168 in order to restart scene with click on that button still not sure how to stop game and scoring when time is up its possible i have some errors in those 2 functions and probably need to add some code for this to work. Can anyone help?

[code]module ( … , package.seeall )

function new( )
local localGroup = display.newGroup ( )

local time_remain = 24
time_up = false
ready = false

local time_remain = 24

local countdowntxt = display.newText( time_remain, 300, 0, native.systemFont, 16 )
countdowntxt:setTextColor ( 255, 0, 0 )

local function countdown (e)
if (time_remain == 24) then
ready = true
end
time_remain =time_remain -1
print(time_remain)
countdowntxt.text = time_remain

if(time_remain == 0) then
time_up = true

end
end

local play_again = display.newImage( “restartbutton.png”, 50, 240)
play_again.isVisible = false
play_again.alpha = 0
play_again.scene = nil

gametimer = timer.performWithDelay ( 1000, countdown, 24 )

–> Add physics engine, start up the engine, and apply gravity
local physics = require (“physics”)
physics.start( )
– Set gravity to act “down” (ie, toward the bottom of the device)
physics.setGravity(0, 9.8)

– physics.setDrawMode(“hybrid”)

–> Add background image
local background = display.newImage(“grid_basketball.jpg”)

localGroup:insert(background)

–> Add ball to stage and position
local ball = display.newImage(“Basketball.png”)
ball.x = display.contentWidth/2
–> Turn ball into physics body
physics.addBody(ball, { bounce = 0.5, radius = 40, friction = 1.0 } )

localGroup:insert(ball)

–> Add ball2
local ball2 = display.newImage(“Basketball.png”)
ball2.x = ball.x - 105
physics.addBody(ball2, {bounce = 0.5, radius = 40, friction = 1.0 } )

localGroup:insert(ball2)

–> Add ball3
local ball3 = display.newImage(“Basketball.png”)
ball3.x = ball.x + 105
physics.addBody (ball3, {bounce = 0.5, radius = 40, friction = 1.0 } )

localGroup:insert(ball3)

system.activate( “multitouch” )

– Define wall graphics (rectangles)
local leftWall = display.newRect(0, 0, 0.1, display.contentHeight )
local rightWall = display.newRect(display.contentWidth, 0, 0.1, display.contentHeight)
local ceiling = display.newRect(0, 0, display.contentWidth, 0.1 )

– Turn wall graphics into physical bodies
physics.addBody( leftWall, “static”, { bounce = 0.1 } )
physics.addBody( rightWall, “static”, { bounce = 0.1 } )
physics.addBody(ceiling, “static”, { bounce = 0.1 } )

–> Add floor image and position
local floor = display.newImage(“floor.png”)
floor.y = display.contentHeight - floor.contentHeight/2
–> Turn floor into physics body
physics.addBody(floor, “static”, { bounce = 0.2, friction = 1.0 } )

localGroup:insert(floor)

– Define our touch event listner
function moveBall(event)
local ball = event.target
ball:applyLinearImpulse( 0, -0.3, event.x, event.y )
end

– Add the listener to our ball

ball:addEventListener(“touch”, moveBall)
ball2:addEventListener(“touch”, moveBall)
ball3:addEventListener(“touch”, moveBall)

–Saving/Loading Stuff
local ego = require “ego”
local saveFile = ego.saveFile
local loadFile = ego.loadFile

– Create score text
local score = 0
local scoreText = display.newText(score, 35, 0, native.systemFont, 16)
scoreText:setTextColor( 255, 255, 255 )
scoreText.text = "score: "…score

– Function to add to score and update scoreText
local function addToScore()
score = score + 1
scoreText.text = "score: "…score
end

–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
print ("Highscore is: ", highscore)
local highscoreText = display.newText(highscore, 200, 0, native.systemFont, 16)
highscoreText:setTextColor( 255, 255, 0 )
highscoreText.text = "Highscore is: "…highscore

–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)
local highscoreText = display.newText(highscore, 200, 0, native.systemFont, 16)
highscoreText:setTextColor( 255, 255, 0)
highscoreText.text = "Highscore is: "…highscore
end
end
Runtime:addEventListener( “system”, onSystemEvent )

ball:addEventListener(“touch”, addToScore)
ball2:addEventListener(“touch”, addToScore)
ball3:addEventListener(“touch”, addToScore)

function changeScene (e)
if (e.phase == " ended" ) then

play_again.isVisible = true

if (e.target.scene) then
– Load game again
time_remain = 24
time_up = false
–Clear the game timer
gametimer = nil
end
end
end

play_again:addeventListener (“touch”, changeScene)

return localGroup
end [import]uid: 189039 topic_id: 32550 reply_id: 332550[/import]

First thing to jump out at me-

Line 179;
[lua]play_again:addeventListener (“touch”, changeScene)[/lua]
Should be;
[lua]play_again:addEventListener (“touch”, changeScene)[/lua]

Peach

[import]uid: 52491 topic_id: 32550 reply_id: 129418[/import]

Yes, that was error but still it does not do what i was planed for it to do.Trying to make it stop physics or touch event on balls once time_remain == 0, and then to show button for play_again but not sure what code to use for that or where to put it.Can anyone help?

[code]local time_remain = 24
time_up = false
ready = false

local time_remain = 24

– Display countdown text
local countdowntxt = display.newText( time_remain, 300, 0, native.systemFont, 16 )
countdowntxt:setTextColor ( 255, 0, 0 )

–Add funtion for countdown
local function countdown (e)
if (time_remain == 24) then
ready = true
end
time_remain =time_remain -1
print(time_remain)
countdowntxt.text = time_remain

if(time_remain == 0) then
time_up = true

end
end

–Add button to play again
local play_again = display.newImage( “restartbutton.png”, 50, 240)
play_again.isVisible = false
play_again.alpha = 0
play_again.scene = nil

gametimer = timer.performWithDelay ( 1000, countdown, 24 )
[/code]
Also play_again button does not appear.

[code]–Function to change scene(play it again)
function changeScene (e)
if (e.phase == " ended" ) then

play_again.isVisible = true
play_again.alpha = 1

if (e.target.scene) then
– Load game again
time_remain = 24
time_up = false
–Clear the game timer
gametimer = nil
end
end
end

play_again:addEventListener (“touch”, changeScene)[/code]
or is it posible to handle this with change scene so it would take to game over screen when time is up? [import]uid: 189039 topic_id: 32550 reply_id: 129441[/import]

I made new game over scene gameover.lua but still cant get it to change scene when time_remain==0
i used this function at end:

[code]–Function to change scene(play it again)
function changeScene(e)
if (e.phase == “ended” ) then
director:changeScene(e.target.scene)
end
end

time_remain:addEventListener (time_up , changeScene)[/code] [import]uid: 189039 topic_id: 32550 reply_id: 129460[/import]

First thing to jump out at me-

Line 179;
[lua]play_again:addeventListener (“touch”, changeScene)[/lua]
Should be;
[lua]play_again:addEventListener (“touch”, changeScene)[/lua]

Peach

[import]uid: 52491 topic_id: 32550 reply_id: 129418[/import]

Yes, that was error but still it does not do what i was planed for it to do.Trying to make it stop physics or touch event on balls once time_remain == 0, and then to show button for play_again but not sure what code to use for that or where to put it.Can anyone help?

[code]local time_remain = 24
time_up = false
ready = false

local time_remain = 24

– Display countdown text
local countdowntxt = display.newText( time_remain, 300, 0, native.systemFont, 16 )
countdowntxt:setTextColor ( 255, 0, 0 )

–Add funtion for countdown
local function countdown (e)
if (time_remain == 24) then
ready = true
end
time_remain =time_remain -1
print(time_remain)
countdowntxt.text = time_remain

if(time_remain == 0) then
time_up = true

end
end

–Add button to play again
local play_again = display.newImage( “restartbutton.png”, 50, 240)
play_again.isVisible = false
play_again.alpha = 0
play_again.scene = nil

gametimer = timer.performWithDelay ( 1000, countdown, 24 )
[/code]
Also play_again button does not appear.

[code]–Function to change scene(play it again)
function changeScene (e)
if (e.phase == " ended" ) then

play_again.isVisible = true
play_again.alpha = 1

if (e.target.scene) then
– Load game again
time_remain = 24
time_up = false
–Clear the game timer
gametimer = nil
end
end
end

play_again:addEventListener (“touch”, changeScene)[/code]
or is it posible to handle this with change scene so it would take to game over screen when time is up? [import]uid: 189039 topic_id: 32550 reply_id: 129441[/import]

I made new game over scene gameover.lua but still cant get it to change scene when time_remain==0
i used this function at end:

[code]–Function to change scene(play it again)
function changeScene(e)
if (e.phase == “ended” ) then
director:changeScene(e.target.scene)
end
end

time_remain:addEventListener (time_up , changeScene)[/code] [import]uid: 189039 topic_id: 32550 reply_id: 129460[/import]

You set your button visibility to false so it wouldn’t show, no. The function you try to set it visible again is a touch listener tied to the play_again button - in other words you need to press the invisible button to make it show up?

[import]uid: 52491 topic_id: 32550 reply_id: 129569[/import]

Can it be set to become visible after certain time, and be set to be unclickable while game is running and i also want to make physics.stop() so im guessing i also need to make physics start again [import]uid: 189039 topic_id: 32550 reply_id: 129575[/import]

You set your button visibility to false so it wouldn’t show, no. The function you try to set it visible again is a touch listener tied to the play_again button - in other words you need to press the invisible button to make it show up?

[import]uid: 52491 topic_id: 32550 reply_id: 129569[/import]

Can it be set to become visible after certain time, and be set to be unclickable while game is running and i also want to make physics.stop() so im guessing i also need to make physics start again [import]uid: 189039 topic_id: 32550 reply_id: 129575[/import]

Yes (timer.performWithDelay), yes (obj.isHitTestable=false), and yes you can make it start again after stopping it if you need/want to (physics.start). [import]uid: 52491 topic_id: 32550 reply_id: 129711[/import]

Yes (timer.performWithDelay), yes (obj.isHitTestable=false), and yes you can make it start again after stopping it if you need/want to (physics.start). [import]uid: 52491 topic_id: 32550 reply_id: 129711[/import]