Hi, i’m new on corona, but i am creating my first game, and i just want to create a timer that start at 10 and ends on 0, so then i can display a text: “GAME OVER”, help please [import]uid: 138440 topic_id: 24680 reply_id: 324680[/import]
[lua]local time = 10; – total time
local count_timer;
function CountDown()
if(time > 1)then
time = time - 1;
– Do whatever else you want here to display countdown time
else
time = 0;
– Display Game Over Here
timer.cancel(count_timer);
end
end
count_timer = timer.performWithDelay(1000, CountDown); – counts down every second[/lua]
That is not tested but give it a shot. That is one of the ffew ways to do it. [import]uid: 63800 topic_id: 24680 reply_id: 99968[/import]
hi thanks for your help.
i copy and paste the code and i print(time)
and only display 199 [import]uid: 138440 topic_id: 24680 reply_id: 99970[/import]
I fixed, thanks so much, i just need to add, the number of times i want to call the method [import]uid: 138440 topic_id: 24680 reply_id: 99971[/import]
Glad to help [import]uid: 63800 topic_id: 24680 reply_id: 99972[/import]
me again, is this possible
local time = 10; – milliseconds
local count_timer;
local timerText = display.newText( "time: " … time,120, 210, Font, 45)
function CountDown()
if(time > 1)then
time = time - 1;
timerText.text = “time:” …time
– Do whatever else you want here to display countdown time
else
time = 0;
timer.cancel(count_timer);
resetlevelforTime()
end
end
count_timer = timer.performWithDelay(1000, CountDown, 10) [import]uid: 138440 topic_id: 24680 reply_id: 99975[/import]
[lua]local time_remaining = 10; – seconds
local time_total = 10;
local count_timer;
function CountDown()
if(time_remaining > 1)then
time_remaining = time_remaining - 1;
– Use time_remaining to display text
else
– Game Over time_remaining ='s 0
– Do Stuff Here
end
end
count_timer = timer.performWithDelay(1000, CountDown, time_total);[/lua] [import]uid: 63800 topic_id: 24680 reply_id: 99976[/import]
You can also use that function as what is referred to as a game loop… All things that you want to check for every second can go into that code. Things such as the status of various enemies, player health, etc. [import]uid: 63800 topic_id: 24680 reply_id: 99978[/import]
oo i get it, another question, how i can stop and resume the timer, i know about timer.pause() but i still lost [import]uid: 138440 topic_id: 24680 reply_id: 99979[/import]
if time\_remaining == 5 then
timer.pause( CountDown )
end
Then resume it depending when you or what you want it to do exactly
[import]uid: 77199 topic_id: 24680 reply_id: 99980[/import]
thank you so much, you rock!!! [import]uid: 138440 topic_id: 24680 reply_id: 99984[/import]
local countDown = 10
local timerText = display.newText( "", 100, 100, native.systemFont, 16)
timerText:setTextColor( 255, 255, 255 )
local function gameOver()
if countDown == 0 then
countDown = 0
timerText.text = countDown
local myText = display.newText("GameOver", 0, 0, native.systemFont, 16)
myText:setTextColor(255, 255, 255)
timer.cancel( countDown )
end
timerText.text = countDown
countDown = countDown - 1
end
timer.performWithDelay( 1000, gameOver, 11 )
Sorry took me so long, different version for your learning experience I guess since someone already posted it, enjoy. [import]uid: 77199 topic_id: 24680 reply_id: 99967[/import]
Also, even though I actually like this code above from hatethinkingofnames
this is a little more complex
http://developer.anscamobile.com/code/stopwatch-timing-class
It’s a “stopwatch” class someone made. I am using it although I think I may ditch that and use your code HTON (that’s your nickname now lol, hate thinkingfonames).
Ok next step with this codeI want to work on:
–Add a touch event (for testing purposes) to stop the timer
–Take the results of the timer, as in X amount of seconds left and write that to a string
–Take the number and write it to a file so it can be used later on for something else like a high score, or whatever.
I’ll come back and update again. I Think this will be useful for others :). My game is built basically, now I am working on all this “back end” stuff like scores, high scores, leader boards etc etc etc. I don’t find it as fun as the actual game, but it needs to be done.
ng [import]uid: 61600 topic_id: 24680 reply_id: 99989[/import]
thanks for the nick, i hate my own name too since its so long.
I havent got into strings yet nor high scores. But for high scores im guessing you just give the current value to another variable that you save on exit or death or whenever you want it too. I agree, making scores and all that little stuff is so boring compared to the rest, but id guess if you’ve done it once or twice it should be pretty easy the following times.
I created a pause and resume button for those interested since I was bored:
[code]
local countDown = 10
pauseTime = false;
resumeTime = true;
local timerText = display.newText( “”, 100, 100, native.systemFont, 16)
timerText:setTextColor( 255, 255, 255 )
local timeBut = display.newRect( 100, 0, 100, 50 )
timeBut:setFillColor( 255, 255, 255 )
physics.addBody( timeBut, “static” )
local buttonText = display.newText ( “”, 150, 25, native.systemFont, 16)
buttonText:setTextColor( 0, 0, 0 )
buttonText.text = “Pause”
function gameOver()
if countDown == 0 then
countDown = 0
currentTime = countDown
timerText.text = “Time:”…countDown
local myText = display.newText(“GameOver”, 0, 0, native.systemFont, 16)
timer.cancel( gameOver )
end
timerText.text = “Time:”…countDown
currentTime = countDown
countDown = countDown - 1
end
countDownTimer = timer.performWithDelay( 1000, gameOver, 11 )
local function timeButton( event )
if event.phase == “began” then
if resumeTime == true then
timer.pause( countDownTimer )
pauseTime = true
resumeTime = false
buttonText.text = “Resume”
elseif pauseTime == true then
timer.resume( countDownTimer )
pauseTime = false
resumeTime = true
buttonText.text = “Pause”
end
end
end
timeBut:addEventListener( “touch”, timeButton )
[/code] [import]uid: 77199 topic_id: 24680 reply_id: 99998[/import]
For the highscores, you can use Ego. Peach Pellen (she’s ansca staff) runs a website called techority.com
Go there search for “ego” It’s SUPER EASY to use. You can save highscores, or whatever file you want. I’m integrating it right now into it. Soon as I do I’ll post.
By time we are done with this thread, there will be a workable mini framework for timer to score, and saving and loading data, that will be neatO.
ng
ps. WeeEeEEeEEeEEEeEEeE!
oh double ps - cool update, the pause unpause was helpful. Bored? How can that be, I got so much coding to do I am not bored, crap I should go code now. Why am I still typing.
must…stop…the…hands.
*EDIT*
OOOOOOOOOOHHHHH. IM TELLING!
Ahhhhhhh. You forgot to add physics. SHAME SHAME SHAME
I Added physics, and also centered the button and made text larger. Yay.
[code]
–YAY PHYSICS!!
local physics = require “physics”
physics.setPositionIterations(60)
physics.setVelocityIterations(60)
physics.start(true)
physics.setGravity( 0, 0 )
physics.setDrawMode( “normal”) --so we can see score, and objects
–DECLARE WIDTH AND HEIGHT SHORTCUT VARIABLES
local _W = display.contentWidth
local _H = display.contentHeight
local countDown = 10
pauseTime = false;
resumeTime = true;
local timerText = display.newText( “”, _W/2 +20,_H/2 -50, native.systemFont, 36)
timerText:setTextColor( 255, 255, 255 )
local timeBut = display.newRect( _W/2-100,_H/2, 200, 80 )
timeBut:setFillColor( 255, 255, 255 )
physics.addBody( timeBut, “static” )
local buttonText = display.newText ( “”, timeBut.x, timeBut.y -20, native.systemFont, 36)
buttonText:setTextColor( 255, 0, 0 )
buttonText.text = “Pause”
function gameOver()
if countDown == 0 then
countDown = 0
currentTime = countDown
timerText.text = “Time:”…countDown
local myText = display.newText(“GameOver”, _W/2 -130,_H/2 - 200, native.systemFont, 56)
myText:setTextColor (255,0,0)
timer.cancel( gameOver )
end
timerText.text = “Time:”…countDown
currentTime = countDown
countDown = countDown - 1
end
countDownTimer = timer.performWithDelay( 1000, gameOver, 11 )
local function timeButton( event )
if event.phase == “began” then
if resumeTime == true then
timer.pause( countDownTimer )
pauseTime = true
resumeTime = false
buttonText.text = “Resume”
elseif pauseTime == true then
timer.resume( countDownTimer )
pauseTime = false
resumeTime = true
buttonText.text = “Pause”
end
end
end
timeBut:addEventListener( “touch”, timeButton )
[/code] [import]uid: 61600 topic_id: 24680 reply_id: 100225[/import]