and when it reaches 5, text dissapears? I will post my code if you like. I tried using a variable nd setting it to 5 so it could countdown to 0 but I don’t think I did it right.
You can use elapsed time which is independent of FPS or you can depend on FPS as I’ve shown out here.
function setup()
timer=60*5 --since codea draws 60 times a second
timer2=5
end
function draw()
background(0, 0, 0, 255)
timer = timer-1
if timer>0 then
text("Hello World! from timer",WIDTH/2,HEIGHT/2)
end
--this is better, as its independent of FPS
if timer2<ElapsedTime then
text("Hello World! from timer2",WIDTH/2,HEIGHT/2-100)
end
end
Or you can add up deltaTime, which gives the time since the last redraw
There are a lot of countdown timers that have been posted in this forum already, but finding them is a challenge. Since I don’t know what your code looks like or how you will use the timer, here is another version.
function setup()
et=0
end
function draw()
background(40, 40, 50)
fill(255)
timer()
if showText then
text("show this text until 5 is reached "..math.ceil(et-ElapsedTime),
WIDTH/2,HEIGHT/2)
end
if not showText then
text("tap screen to start timer",WIDTH/2,HEIGHT/2)
end
end
function timer(s)
if s~=nil then
et=ElapsedTime+s
showText=true
end
if et-5<ElapsedTime then
showText=false
end
end
function touched(t)
if t.state==BEGAN then
timer(15)
end
end
@dave1707 I tried using your code and it works, but I modded it to make it work for my menu. Finally got it to work after about 10-20 minutes. Here’s the code for interest matters:
ps. sorry about the messy layout. I don’t know how to make it all format correctly
--I had to create 2 extra booleans that stored the required text for my menu
--deleted the loading countdown as it was causing 90% of the trouble
--when you load the game, the fuel and gravity are set to 0 so that you cannot move it
function draw()
--This sets a dark background color
background(40, 40, 50)
timer()
if not showText then
title=true
if title then
text("space Thunder", WIDTH/2, 550)
font("Courier-Bold")
fontSize(30)
gravity=0
fuel=0
subtitle=true
if subtitle then
text("Created by One Blaster Games", WIDTH/2, 500)
end
loadit=true
if loadit then
text("Touch screen to start", WIDTH/2, 100)
gravity=0
fuel=0
end
end
end
--here I set the gravity and fuel back to their regular numbers so when you
--touch the screen, the game starts
function touched(t)
if t.state==BEGAN then
timer(15)
gravity=-0.02
fuel=100
end
end
Please read the FAQ for how to format code on the forum.
@Andrew_Stacy just did
@Kingamer, pardon my confusion. In your code, you called timer() after you set the dark background, yet I don’t see a timer() function defined. Are you using the timer() function from @Dave1707’s code?
@Ric_Esrey in function(touched) I set timer to 15 after you touch the screen.
But other than that I don’t really have much of a definition. Yes, I took snippets of Dave1707’s code cuz this discussion was howto make a countdown timer, but I’m way over that now.
@Kingamer, I may not have been very clear. Your code doesn’t work when I copy and paste it into Codea. I believe the problem is that you call timer(), yet you don’t have a timer function in your code. If you used @Dave1707’s timer function, you should include that code segment in your version so that when beginners (like me) paste it into Codea, it works. For instance, this is an example of your code with the addition of @Dave1707’s timer() function:
function setup()
et = 0
end
function draw()
--This sets a dark background color
background(40, 40, 50)
timer()
if not showText then
title=true
if title then
text("space Thunder", WIDTH/2, 550)
font("Courier-Bold")
fontSize(30)
gravity=0
fuel=0
end
subtitle=true
if subtitle then
text("Created by One Blaster Games", WIDTH/2, 500)
end
loadit=true
if loadit then
text("Touch screen to start", WIDTH/2, 100)
gravity=0
fuel=0
end
end
end
function timer(s) --********I added the missing timer function***********
if s~=nil then
et=ElapsedTime+s
showText=true
end
if et-5<ElapsedTime then
showText=false
end
end
--here I set the gravity and fuel back to their regular numbers so when you
--touch the screen, the game starts
function touched(t)
if t.state==BEGAN then
timer(15)
gravity=-0.02
fuel=100
end
end