How to change screen when game time =0?

So i want my game to change scene to if condition time_remaining==0 or time_up = true.
How can i do that and what code to use? I tried doing so with this code but it didnt work well:

[code] 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

gametimer = timer.performWithDelay ( 1000, countdown, 24 )
[/code]

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

countdown:addEventListener (time_remain== 0 , changeScene)
[/code] [import]uid: 189039 topic_id: 32569 reply_id: 332569[/import]

Uh…usually you need to say if you’re using Director since a lot of folks use storyboard now instead.

Anyway, I would probably write it more like this…

[code]
local time = { left=300, max=300 }

– Make the display object
– The first value you list (“time_remain”) needs to be a display group. Not sure why you have it there
local gameTime = display.newImage(time.left, 0, native.systemFont, 16)

– Make the countdown function; this makes it part of the gameTime table!
function gameTime.countDown()
if time.left > 0 then
if time.left == 24 then gameTime.ready = true end – no idea what this is for, left it in
time.left = time.left - 1
gameTime.text = time.left
else – time is at zero so no need to change the time anymore
gameTime.timeUp = true
end
end --/gameTime.countDown()

– By making the timer part of the display object table you can easily cancel it if needed
gameTime.timer = timer.performWithDelay(1000, gameTime.countDown)[/code]

The part I don’t understand is the code at the end and how you’re determining where to go next. If time is up and you always go back to the same screen, you could just add the director scenechange into the section with the ELSE statement. If you’re not sure where to go I would do something like this:

-- Theoretical touch function for some other object function level2button.touch(event) if event.phase == "ended" then gameTime.targetScene = "level2" end end level2button:addEventListener("touch", level2button)

That way you could then change the director code to point at the variable gameTime.targetScene. But it really depends on how you want to implement it. [import]uid: 41884 topic_id: 32569 reply_id: 129475[/import]

I’m using director class, and what im trying to do is after time is up to take it to next screen (game over screen) and there to put option to restart game. there is no level 2.Or if times up to stop my game and give option to restart, like button would pop up when time is up.
this is my game:

[code]_W = display.contentWidth
_H = display.contentHeight

module ( … , package.seeall )

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

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

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 )

– Add listeners to balls so it will add to score when they are touch
ball:addEventListener(“touch”, addToScore)
ball2:addEventListener(“touch”, addToScore)
ball3:addEventListener(“touch”, addToScore)

–Function to change scene
function changeScene(e)
if (e.phase == “ended” ) then
director:changeScene(e.target.scene)
end
end

countdown:addEventListener (time_remain== 0 , changeScene)

return localGroup
end
[/code]
and this is game over screen

[code]module ( … , package.seeall )

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

local background = display.newImage( “game-over.jpg” )

–Add button to play again
local play_again = display.newImage( “restartbutton.png”, 50, 240)
play_again.scene = “game”

function changeScene(e)
if ( e.phase == “ended”) then
director:changeScene(e.target.scene)
end
end

localGroup:insert(background)
localGroup:insert(play_again)

play_again:addEventListener ( “touch” , changeScene )

return localGroup
end
[/code]Can screen transition be done without touch event? If not how can i stop my game after game time is up and make button to appear after game is done to restart it and i dont want this button to be displayed on my screen while game is running? [import]uid: 189039 topic_id: 32569 reply_id: 129479[/import]

Uh…usually you need to say if you’re using Director since a lot of folks use storyboard now instead.

Anyway, I would probably write it more like this…

[code]
local time = { left=300, max=300 }

– Make the display object
– The first value you list (“time_remain”) needs to be a display group. Not sure why you have it there
local gameTime = display.newImage(time.left, 0, native.systemFont, 16)

– Make the countdown function; this makes it part of the gameTime table!
function gameTime.countDown()
if time.left > 0 then
if time.left == 24 then gameTime.ready = true end – no idea what this is for, left it in
time.left = time.left - 1
gameTime.text = time.left
else – time is at zero so no need to change the time anymore
gameTime.timeUp = true
end
end --/gameTime.countDown()

– By making the timer part of the display object table you can easily cancel it if needed
gameTime.timer = timer.performWithDelay(1000, gameTime.countDown)[/code]

The part I don’t understand is the code at the end and how you’re determining where to go next. If time is up and you always go back to the same screen, you could just add the director scenechange into the section with the ELSE statement. If you’re not sure where to go I would do something like this:

-- Theoretical touch function for some other object function level2button.touch(event) if event.phase == "ended" then gameTime.targetScene = "level2" end end level2button:addEventListener("touch", level2button)

That way you could then change the director code to point at the variable gameTime.targetScene. But it really depends on how you want to implement it. [import]uid: 41884 topic_id: 32569 reply_id: 129475[/import]

Yeah, you don’t need a touch to execute a scene change. You just need the game to detect that a change has occurred. There are two ways to go about this:

  1. Use a runtime listener. This will check every frame (that’s a lot!!) to see if time equals zero.
  2. Use a custom event. This will send an event out every second until time expires, which can be a lot but is still much cheaper than 60 times a second (that enterFrame uses). It’s more complicated though.

-- Runtime example -- Runtime is basically the corona backbone object that runs your game. local function checkTime(event) if time.left == 0 then -- remove listener -- put your scene change code here end end --/checkTime() Runtime:addEventListener("enterFrame", checkTime)

That’s the simple way of going about it, though you need to remember to remove the listener when you switch scenes. You could also try custom events, though that’s a bit more work. Corona has a pretty good tutorial for it on the blog page. [import]uid: 41884 topic_id: 32569 reply_id: 129519[/import]

My error is probably in one of 2 functions shown in first post or maybe i need to add something to my code.
edit: fixed code in this 2 functions and now i dont get any error

[code]–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

gametimer = timer.performWithDelay ( 1000, countdown, 24 )
[/code]

[code] --Function to change scene
function changeScene(e)
if (e.phase == “ended” ) then
director:changeScene(“gameover”)
end
end

countdowntxt:addEventListener (time_remain== 0 , changeScene)
[/code] but still it does not change scene to gameover.lua when time is up what do i need to add for this to work? [import]uid: 189039 topic_id: 32569 reply_id: 129489[/import]

do i put this instead of my changeScene function? [import]uid: 189039 topic_id: 32569 reply_id: 129522[/import]

I’m using director class, and what im trying to do is after time is up to take it to next screen (game over screen) and there to put option to restart game. there is no level 2.Or if times up to stop my game and give option to restart, like button would pop up when time is up.
this is my game:

[code]_W = display.contentWidth
_H = display.contentHeight

module ( … , package.seeall )

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

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

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 )

– Add listeners to balls so it will add to score when they are touch
ball:addEventListener(“touch”, addToScore)
ball2:addEventListener(“touch”, addToScore)
ball3:addEventListener(“touch”, addToScore)

–Function to change scene
function changeScene(e)
if (e.phase == “ended” ) then
director:changeScene(e.target.scene)
end
end

countdown:addEventListener (time_remain== 0 , changeScene)

return localGroup
end
[/code]
and this is game over screen

[code]module ( … , package.seeall )

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

local background = display.newImage( “game-over.jpg” )

–Add button to play again
local play_again = display.newImage( “restartbutton.png”, 50, 240)
play_again.scene = “game”

function changeScene(e)
if ( e.phase == “ended”) then
director:changeScene(e.target.scene)
end
end

localGroup:insert(background)
localGroup:insert(play_again)

play_again:addEventListener ( “touch” , changeScene )

return localGroup
end
[/code]Can screen transition be done without touch event? If not how can i stop my game after game time is up and make button to appear after game is done to restart it and i dont want this button to be displayed on my screen while game is running? [import]uid: 189039 topic_id: 32569 reply_id: 129479[/import]

Yeah, you don’t need a changeScene custom function. You can just put your director change scene command inside the time.left == 0 section as marked [import]uid: 41884 topic_id: 32569 reply_id: 129525[/import]

that worked now it switches to gameover screen but im not sure what listener should i remove because it keeps sending me back to gameover screen after second when i click to restart game i think thats problem that i didnt remove listener:

local function checkTime(e)  
 if time\_remain == 0 then  
 Runtime:removeEventListener()  
 director:changeScene("gameover")  
 end  
 end   
 Runtime:addEventListener("enterFrame", checkTime)  

Also how can i print current score and highscore on gameover page [import]uid: 189039 topic_id: 32569 reply_id: 129532[/import]

You have to type the specific listener to remove. Runtime can have as many listeners as you want so you have to be specific…

Runtime:removeEventListener("enterFrame", checkTime)

For score the simplest method is to make a global variable in main.lua, that way all of your lua files can see it.

[code]_G.scores = { current=0, high=100000 }
– every file in your project can see the scores Table now.
– _G. variables are much slower than normal variables but you’re not changing this 30 times a second
– Just make sure not to call anything else “scores”
– You only declare _G once.

– to update the table…
scores.current = 3000
scores.high = 99999
– whatever you like
[/code]
[import]uid: 41884 topic_id: 32569 reply_id: 129545[/import]

Ok, so if _G is declared only once then i cant make one for score and one for highscore? [import]uid: 189039 topic_id: 32569 reply_id: 129550[/import]

Yeah, you don’t need a touch to execute a scene change. You just need the game to detect that a change has occurred. There are two ways to go about this:

  1. Use a runtime listener. This will check every frame (that’s a lot!!) to see if time equals zero.
  2. Use a custom event. This will send an event out every second until time expires, which can be a lot but is still much cheaper than 60 times a second (that enterFrame uses). It’s more complicated though.

-- Runtime example -- Runtime is basically the corona backbone object that runs your game. local function checkTime(event) if time.left == 0 then -- remove listener -- put your scene change code here end end --/checkTime() Runtime:addEventListener("enterFrame", checkTime)

That’s the simple way of going about it, though you need to remember to remove the listener when you switch scenes. You could also try custom events, though that’s a bit more work. Corona has a pretty good tutorial for it on the blog page. [import]uid: 41884 topic_id: 32569 reply_id: 129519[/import]

My error is probably in one of 2 functions shown in first post or maybe i need to add something to my code.
edit: fixed code in this 2 functions and now i dont get any error

[code]–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

gametimer = timer.performWithDelay ( 1000, countdown, 24 )
[/code]

[code] --Function to change scene
function changeScene(e)
if (e.phase == “ended” ) then
director:changeScene(“gameover”)
end
end

countdowntxt:addEventListener (time_remain== 0 , changeScene)
[/code] but still it does not change scene to gameover.lua when time is up what do i need to add for this to work? [import]uid: 189039 topic_id: 32569 reply_id: 129489[/import]

do i put this instead of my changeScene function? [import]uid: 189039 topic_id: 32569 reply_id: 129522[/import]

No you can use _G. as much as you want, just only use it when making the variable, not when changing it. It’s the same as how you only say “local” when you make the variable. [import]uid: 41884 topic_id: 32569 reply_id: 129558[/import]

Yeah, you don’t need a changeScene custom function. You can just put your director change scene command inside the time.left == 0 section as marked [import]uid: 41884 topic_id: 32569 reply_id: 129525[/import]

that worked now it switches to gameover screen but im not sure what listener should i remove because it keeps sending me back to gameover screen after second when i click to restart game i think thats problem that i didnt remove listener:

local function checkTime(e)  
 if time\_remain == 0 then  
 Runtime:removeEventListener()  
 director:changeScene("gameover")  
 end  
 end   
 Runtime:addEventListener("enterFrame", checkTime)  

Also how can i print current score and highscore on gameover page [import]uid: 189039 topic_id: 32569 reply_id: 129532[/import]

You have to type the specific listener to remove. Runtime can have as many listeners as you want so you have to be specific…

Runtime:removeEventListener("enterFrame", checkTime)

For score the simplest method is to make a global variable in main.lua, that way all of your lua files can see it.

[code]_G.scores = { current=0, high=100000 }
– every file in your project can see the scores Table now.
– _G. variables are much slower than normal variables but you’re not changing this 30 times a second
– Just make sure not to call anything else “scores”
– You only declare _G once.

– to update the table…
scores.current = 3000
scores.high = 99999
– whatever you like
[/code]
[import]uid: 41884 topic_id: 32569 reply_id: 129545[/import]

Ok, so if _G is declared only once then i cant make one for score and one for highscore? [import]uid: 189039 topic_id: 32569 reply_id: 129550[/import]