Cant get game countdown to work?

Cant get timer in my game to work this is code I used:

[code]local function countDown (e)
if (time_remain == 24) then
ready = true
end
time_remain =time_remain -1
countdowntxt.text = time_remain
end
local countdowntxt = display.newText( time_remain, 300, 0, native.systemFont, 16 )
gametimer = timer.performWithDelay ( 1000, countdown, 24 ) [import]uid: 189039 topic_id: 32537 reply_id: 332537[/import]

and here is my game

[code]module ( … , package.seeall )

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

time_remain = 24
time_up = false
ready = false

–> 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)

local function countDown (e)
if (time_remain == 24) then
ready = true
end
time_remain =time_remain -1
countdowntxt.text = time_remain
end
local countdowntxt = display.newText( time_remain, 300, 0, native.systemFont, 16 )
gametimer = timer.performWithDelay ( 1000, countdown, 24 )

–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, 255 )
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, 255 )
highscoreText.text = "Highscore is: "…highscore
end
end
Runtime:addEventListener( “system”, onSystemEvent )

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

return localGroup
end[/code]
I’m trying to make it count down from 24 to 0 and then to stop game and give option for restart

[import]uid: 189039 topic_id: 32537 reply_id: 129364[/import]

Well you never initialize time_remain and it’s nil when you try to create the display.newText() and it’s throwing this error:

 (/Users/rmiracle/Library/Application Support/Corona Simulator/tmp-292D2A5488D376935C88DB31A32368A3)  
2012-10-31 15:27:48.667 Corona Simulator[3885:f03] Runtime error  
 /Users/rmiracle/tmp/main.lua:8: bad argument #1 to 'newText' (string expected, got nil)  
stack traceback:  
 [C]: ?  
 [C]: in function 'newText'  
 /Users/rmiracle/tmp/main.lua:8: in main chunk  
2012-10-31 15:27:48.673 Corona Simulator[3885:f03] Runtime error:   
2012-10-31 15:27:48.674 Corona Simulator[3885:f03] /Users/rmiracle/tmp/main.lua:8: bad argument #1 to 'newText' (string expected, got nil)  
stack traceback:  
 [C]: ?  
 [C]: in function 'newText'  
 /Users/rmiracle/tmp/main.lua:8: in main chunk  

Try adding:

local time_remain = 24

at the top. There are several other problems as well. First your function is called “count D down” but you’re calling “count d own” and so your timer doesn’t have anything to do.

Finally, countdowntxt is defined after you try to use it in the countDown function. Lua doesn’t have look ahead in its compiler. It’s a single pass type. You have to declare things before you use them.

This code works:

local time\_remain = 24  
  
local countdowntxt = display.newText( time\_remain, 300, 0, native.systemFont, 32 )  
  
local function countdown (e)  
 if (time\_remain == 24) then  
 ready = true  
 end  
 time\_remain =time\_remain -1  
 print(time\_remain)  
 countdowntxt.text = time\_remain  
end  
gametimer = timer.performWithDelay ( 1000, countdown, 24 )  

[import]uid: 19626 topic_id: 32537 reply_id: 129367[/import]

yep that worked , now only how to stop game at
if (time_remain == 0) then
– condition to stop game
– and give option to restart it [import]uid: 189039 topic_id: 32537 reply_id: 129375[/import]

You know where you have that:

if time_remain == 24 then ???

that would be a good place to stop all your physics and load your game over screen or do whatever processing you need to do. You have it right:

if time\_remain == 0 then  
 -- do your level/game over bits here  
end  

right after you subtract 1 from time_remain [import]uid: 19626 topic_id: 32537 reply_id: 129384[/import]

not sure where to put 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

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

[import]uid: 189039 topic_id: 32537 reply_id: 129399[/import]

and here is my game

[code]module ( … , package.seeall )

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

time_remain = 24
time_up = false
ready = false

–> 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)

local function countDown (e)
if (time_remain == 24) then
ready = true
end
time_remain =time_remain -1
countdowntxt.text = time_remain
end
local countdowntxt = display.newText( time_remain, 300, 0, native.systemFont, 16 )
gametimer = timer.performWithDelay ( 1000, countdown, 24 )

–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, 255 )
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, 255 )
highscoreText.text = "Highscore is: "…highscore
end
end
Runtime:addEventListener( “system”, onSystemEvent )

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

return localGroup
end[/code]
I’m trying to make it count down from 24 to 0 and then to stop game and give option for restart

[import]uid: 189039 topic_id: 32537 reply_id: 129364[/import]

Well you never initialize time_remain and it’s nil when you try to create the display.newText() and it’s throwing this error:

 (/Users/rmiracle/Library/Application Support/Corona Simulator/tmp-292D2A5488D376935C88DB31A32368A3)  
2012-10-31 15:27:48.667 Corona Simulator[3885:f03] Runtime error  
 /Users/rmiracle/tmp/main.lua:8: bad argument #1 to 'newText' (string expected, got nil)  
stack traceback:  
 [C]: ?  
 [C]: in function 'newText'  
 /Users/rmiracle/tmp/main.lua:8: in main chunk  
2012-10-31 15:27:48.673 Corona Simulator[3885:f03] Runtime error:   
2012-10-31 15:27:48.674 Corona Simulator[3885:f03] /Users/rmiracle/tmp/main.lua:8: bad argument #1 to 'newText' (string expected, got nil)  
stack traceback:  
 [C]: ?  
 [C]: in function 'newText'  
 /Users/rmiracle/tmp/main.lua:8: in main chunk  

Try adding:

local time_remain = 24

at the top. There are several other problems as well. First your function is called “count D down” but you’re calling “count d own” and so your timer doesn’t have anything to do.

Finally, countdowntxt is defined after you try to use it in the countDown function. Lua doesn’t have look ahead in its compiler. It’s a single pass type. You have to declare things before you use them.

This code works:

local time\_remain = 24  
  
local countdowntxt = display.newText( time\_remain, 300, 0, native.systemFont, 32 )  
  
local function countdown (e)  
 if (time\_remain == 24) then  
 ready = true  
 end  
 time\_remain =time\_remain -1  
 print(time\_remain)  
 countdowntxt.text = time\_remain  
end  
gametimer = timer.performWithDelay ( 1000, countdown, 24 )  

[import]uid: 19626 topic_id: 32537 reply_id: 129367[/import]

yep that worked , now only how to stop game at
if (time_remain == 0) then
– condition to stop game
– and give option to restart it [import]uid: 189039 topic_id: 32537 reply_id: 129375[/import]

You know where you have that:

if time_remain == 24 then ???

that would be a good place to stop all your physics and load your game over screen or do whatever processing you need to do. You have it right:

if time\_remain == 0 then  
 -- do your level/game over bits here  
end  

right after you subtract 1 from time_remain [import]uid: 19626 topic_id: 32537 reply_id: 129384[/import]

not sure where to put 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

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

[import]uid: 189039 topic_id: 32537 reply_id: 129399[/import]