I can't understand timers... or it's a bug :)

I can’t understand why after the execution of this code (it stops when it reachs 360 ms) i have 360 ms and 10 seconds.

It seems like 36 ms are 1 second? How can be it possible?

Does the “problem” it’s caused by frames? I don’t think so, because they are arithmetic operations…
[lua]function sumarsegundo( event )

–+1 second
segundos = segundos + 1
texto.text = "Segundos: "…segundos

end

function sumarmilisegundo( event )

–+1 ms
milisegundos = milisegundos + 1
texto2.text = "Milisegundos: "…milisegundos

end

tmr2 = timer.performWithDelay( 1000, sumarsegundo, 0 )
tmr3 = timer.performWithDelay( 1, sumarmilisegundo, 0 )[/lua]

PS: I didn’t see any min of the milliseconds in the docs. [import]uid: 116842 topic_id: 33995 reply_id: 333995[/import]

Hello,
I think the issue is, in fact, “frames”. No mobile app responds in cycles as fast as 1 millisecond. You can tell a Corona app to run at 60 FPS (frames-per-second), and so the math works like this:

1 second = 1000 milliseconds
1000 / 60 = 16.66666667

Thus, if you use a timer at 1 millisecond, the actual timer won’t execute until the next cycle, which is 16.66666667 milliseconds from the previous one.

Hope this helps clarify somewhat!
Brent

[import]uid: 200026 topic_id: 33995 reply_id: 135143[/import]

WoW, a staff reply me, I feel good!! :wink:

I was thinking frames were just a “visual” problem. Didn’t expect that, in operations too. But thanks. I’ll play with fps.

I’m doing a timer, and i was trying to make how long i want the analog-clock to stay. Do you have any idea?

PS: I don’t know if there is a better ( well, i know there will be a better way, but i can’t think other ) way for doing this. It’s a plug-and-play code btw.

Thanks.

[lua]–RELOJ

local lineas = {}
local segundos = -1
local milisegundos = -1
local texto = display.newText( "Segundos: ", display.contentWidth/4, display.contentHeight/7-50, “Arial”, 34 )
local texto2 = display.newText( "Milisegundos: ", display.contentWidth/4-50, display.contentHeight-100, “Arial”, 34 )
for i=1, 360, 1 do
local secondHand = display.newLine( display.contentWidth/2, display.contentHeight/2,
display.contentWidth/2+100, display.contentHeight/2+100 )
secondHand:setColor( 255, 102, 102 )
secondHand.width = 3
secondHand:setReferencePoint( display.LeftReferencePoint )
secondHand.rotation = i*1
table.insert ( lineas, secondHand )
end
function borrarlinea( event )

–algún semaforo para controlar el tiempo?

if table.maxn( lineas ) ~= 0 then
print( “borrando” )
print( "rotacion: "…lineas[#lineas].rotation )
–lineas[#lineas].isvisible = false
lineas[#lineas]:removeSelf()
–lineas[#lineas] = nil
–lineas[#lineas].width = 0
table.remove ( lineas, table.maxn( lineas ) )
else
timer.cancel( tmr2 )
timer.cancel( tmr3 )
end
end

function sumarsegundo( event )

segundos = segundos + 1
texto.text = "Segundos: "…segundos

end

function sumarmilisegundo( event )

milisegundos = milisegundos + 1
texto2.text = "Milisegundos: "…milisegundos

end

tmr = timer.performWithDelay( 1, borrarlinea, 0 )
–Runtime:addEventListener( “enterFrame”, borrarlinea )
tmr2 = timer.performWithDelay( 1000, sumarsegundo, 0 )
tmr3 = timer.performWithDelay( 1, sumarmilisegundo, 0 )[/lua] [import]uid: 116842 topic_id: 33995 reply_id: 135145[/import]

Hello @correogracioso,
Happy to help! Do you want to design an analog clock that looks like this?

http://images.apple.com/downloads/dashboard/status/images/biganalogclock_20070608165330.jpg

If so, I can provide you with a more complete example.

Gracias,
Brent
[import]uid: 200026 topic_id: 33995 reply_id: 135173[/import]

Not exactly. I call it analog clock because i haven’t got a big vocabulary.

I’m trying to make something like this:

http://youtu.be/ql8F7W_psFk

A bonus score template from deepblueapps. At the bottom right, that “clock”.

When i say ms, i want to say cycles, how you call it.
It’s like, the circle has 360 lines. I deduct 1 line each ms. But it isn’t 360 ms, they are 10000 msecs ( 10 seconds ). I can deduct 1 each 30 ms, but it isn’t 10800, it is 270 ms.

Resume: i want to make my “clock”/score dynamic. So it can be 5 sec of duration, or 20.

And thanks Brent with your time. [import]uid: 116842 topic_id: 33995 reply_id: 135178[/import]

Hello again,
I think I have a solution for you, but it’s different than your current sample.

May I ask, will this “clock” item be placed in front of another image on the screen, or only a solid-color background?

Brent [import]uid: 200026 topic_id: 33995 reply_id: 135185[/import]

( I think ) I don’t care, because i can adapt with any code given. I just don’t know how to do that, “play with time”. Thanks [import]uid: 116842 topic_id: 33995 reply_id: 135187[/import]

Hello again,
I created a sample project for you (link below). This uses 4 images, not 360 "newLine"s. It also uses the Corona transition, not a timer. Hopefully you can use this in your project! :slight_smile:

https://www.dropbox.com/s/ftu4pa99ogvk4oj/clocktimer.zip

You will need to create new images (very simple) for the correct size “clock” that you need.

Take care,
Brent [import]uid: 200026 topic_id: 33995 reply_id: 135294[/import]

Hello,
I think the issue is, in fact, “frames”. No mobile app responds in cycles as fast as 1 millisecond. You can tell a Corona app to run at 60 FPS (frames-per-second), and so the math works like this:

1 second = 1000 milliseconds
1000 / 60 = 16.66666667

Thus, if you use a timer at 1 millisecond, the actual timer won’t execute until the next cycle, which is 16.66666667 milliseconds from the previous one.

Hope this helps clarify somewhat!
Brent

[import]uid: 200026 topic_id: 33995 reply_id: 135143[/import]

WoW, a staff reply me, I feel good!! :wink:

I was thinking frames were just a “visual” problem. Didn’t expect that, in operations too. But thanks. I’ll play with fps.

I’m doing a timer, and i was trying to make how long i want the analog-clock to stay. Do you have any idea?

PS: I don’t know if there is a better ( well, i know there will be a better way, but i can’t think other ) way for doing this. It’s a plug-and-play code btw.

Thanks.

[lua]–RELOJ

local lineas = {}
local segundos = -1
local milisegundos = -1
local texto = display.newText( "Segundos: ", display.contentWidth/4, display.contentHeight/7-50, “Arial”, 34 )
local texto2 = display.newText( "Milisegundos: ", display.contentWidth/4-50, display.contentHeight-100, “Arial”, 34 )
for i=1, 360, 1 do
local secondHand = display.newLine( display.contentWidth/2, display.contentHeight/2,
display.contentWidth/2+100, display.contentHeight/2+100 )
secondHand:setColor( 255, 102, 102 )
secondHand.width = 3
secondHand:setReferencePoint( display.LeftReferencePoint )
secondHand.rotation = i*1
table.insert ( lineas, secondHand )
end
function borrarlinea( event )

–algún semaforo para controlar el tiempo?

if table.maxn( lineas ) ~= 0 then
print( “borrando” )
print( "rotacion: "…lineas[#lineas].rotation )
–lineas[#lineas].isvisible = false
lineas[#lineas]:removeSelf()
–lineas[#lineas] = nil
–lineas[#lineas].width = 0
table.remove ( lineas, table.maxn( lineas ) )
else
timer.cancel( tmr2 )
timer.cancel( tmr3 )
end
end

function sumarsegundo( event )

segundos = segundos + 1
texto.text = "Segundos: "…segundos

end

function sumarmilisegundo( event )

milisegundos = milisegundos + 1
texto2.text = "Milisegundos: "…milisegundos

end

tmr = timer.performWithDelay( 1, borrarlinea, 0 )
–Runtime:addEventListener( “enterFrame”, borrarlinea )
tmr2 = timer.performWithDelay( 1000, sumarsegundo, 0 )
tmr3 = timer.performWithDelay( 1, sumarmilisegundo, 0 )[/lua] [import]uid: 116842 topic_id: 33995 reply_id: 135145[/import]

Hello @correogracioso,
Happy to help! Do you want to design an analog clock that looks like this?

http://images.apple.com/downloads/dashboard/status/images/biganalogclock_20070608165330.jpg

If so, I can provide you with a more complete example.

Gracias,
Brent
[import]uid: 200026 topic_id: 33995 reply_id: 135173[/import]

Not exactly. I call it analog clock because i haven’t got a big vocabulary.

I’m trying to make something like this:

http://youtu.be/ql8F7W_psFk

A bonus score template from deepblueapps. At the bottom right, that “clock”.

When i say ms, i want to say cycles, how you call it.
It’s like, the circle has 360 lines. I deduct 1 line each ms. But it isn’t 360 ms, they are 10000 msecs ( 10 seconds ). I can deduct 1 each 30 ms, but it isn’t 10800, it is 270 ms.

Resume: i want to make my “clock”/score dynamic. So it can be 5 sec of duration, or 20.

And thanks Brent with your time. [import]uid: 116842 topic_id: 33995 reply_id: 135178[/import]

Hello again,
I think I have a solution for you, but it’s different than your current sample.

May I ask, will this “clock” item be placed in front of another image on the screen, or only a solid-color background?

Brent [import]uid: 200026 topic_id: 33995 reply_id: 135185[/import]

( I think ) I don’t care, because i can adapt with any code given. I just don’t know how to do that, “play with time”. Thanks [import]uid: 116842 topic_id: 33995 reply_id: 135187[/import]

I forgot to say thanks yesterday, sorry.

It’s a “funny” way of doing this, but rectangles are so big, that go away from the circle ( the mask ). If you use a background with color, you can see how they move.

So I’m still thinking in other way, or trying to fix mine. Because what you said:
1 second = 1000 milliseconds
1000 / 60 = 16.66666667

It doesn’t work. They aren’t “real” cycles. And sometimes it’s 10 secs, and others 9, for example. And i’m doing it with 100 by 100 ms in the new code…

Btw, your spanish is ok :wink: and thanks you so much. [import]uid: 116842 topic_id: 33995 reply_id: 135508[/import]

Hello again,
I created a sample project for you (link below). This uses 4 images, not 360 "newLine"s. It also uses the Corona transition, not a timer. Hopefully you can use this in your project! :slight_smile:

https://www.dropbox.com/s/ftu4pa99ogvk4oj/clocktimer.zip

You will need to create new images (very simple) for the correct size “clock” that you need.

Take care,
Brent [import]uid: 200026 topic_id: 33995 reply_id: 135294[/import]

Hello again,
Yes, my method is “funny” and it won’t work on a colored background. :slight_smile:

However, I think you can get it working on any background if you use “masking” on the rectangles. This would hide the outside parts. If you want to explore this, a tutorial is here:
http://www.coronalabs.com/blog/2012/05/29/how-to-use-bitmap-masks/

Best of luck!
Brent [import]uid: 200026 topic_id: 33995 reply_id: 135632[/import]

I forgot to say thanks yesterday, sorry.

It’s a “funny” way of doing this, but rectangles are so big, that go away from the circle ( the mask ). If you use a background with color, you can see how they move.

So I’m still thinking in other way, or trying to fix mine. Because what you said:
1 second = 1000 milliseconds
1000 / 60 = 16.66666667

It doesn’t work. They aren’t “real” cycles. And sometimes it’s 10 secs, and others 9, for example. And i’m doing it with 100 by 100 ms in the new code…

Btw, your spanish is ok :wink: and thanks you so much. [import]uid: 116842 topic_id: 33995 reply_id: 135508[/import]

Hello again,
Yes, my method is “funny” and it won’t work on a colored background. :slight_smile:

However, I think you can get it working on any background if you use “masking” on the rectangles. This would hide the outside parts. If you want to explore this, a tutorial is here:
http://www.coronalabs.com/blog/2012/05/29/how-to-use-bitmap-masks/

Best of luck!
Brent [import]uid: 200026 topic_id: 33995 reply_id: 135632[/import]