[RESOLVED] Timer

Hello! I want to make a counter to track time inside a game. How can I do it?
I made this but the new “old” time appears behind the new time. Thanks:)

[lua]local x = 0
local function listener( event )
x = x + 1
local countingSeconds = display.newText(x, 0,0, Verdana, 16*2);
timer.performWithDelay(1000, listener)
end
end

timer.performWithDelay(1000, listener )
[import]uid: 208318 topic_id: 35591 reply_id: 335591[/import]

Only create the display.newText() once outside the function.

Inside the function do:

countingSeconds.text = x

Right now you’re creating a new display object every the timer fires and you never get rid of the old one.
[import]uid: 199310 topic_id: 35591 reply_id: 141443[/import]

Hey Rob! Thanks for your answer. If I create the display.newText() outside the function it will be displayed only once?

[lua]local x = 0
local function listener( event )
x = x + 1
countingSeconds.text = x
timer.performWithDelay(1000, listener)
end

local countingSeconds = display.newText(x, 0,0, Verdana, 16*2);

timer.performWithDelay(1000, listener ) [import]uid: 208318 topic_id: 35591 reply_id: 141445[/import]

You probably need to move the

local countingSeconds = display.newText(x, 0,0, Verdana, 16*2);

above the function. Also do you have a variable called Verdana? If not it needs to be in quotes. That font is also not on most devices. [import]uid: 199310 topic_id: 35591 reply_id: 141447[/import]

Thank you so much. Now everything works. I build for android and added the font in the folder. Also, added quotes. Thanks again:) [import]uid: 208318 topic_id: 35591 reply_id: 141450[/import]

Only create the display.newText() once outside the function.

Inside the function do:

countingSeconds.text = x

Right now you’re creating a new display object every the timer fires and you never get rid of the old one.
[import]uid: 199310 topic_id: 35591 reply_id: 141443[/import]

Hey Rob! Thanks for your answer. If I create the display.newText() outside the function it will be displayed only once?

[lua]local x = 0
local function listener( event )
x = x + 1
countingSeconds.text = x
timer.performWithDelay(1000, listener)
end

local countingSeconds = display.newText(x, 0,0, Verdana, 16*2);

timer.performWithDelay(1000, listener ) [import]uid: 208318 topic_id: 35591 reply_id: 141445[/import]

You probably need to move the

local countingSeconds = display.newText(x, 0,0, Verdana, 16*2);

above the function. Also do you have a variable called Verdana? If not it needs to be in quotes. That font is also not on most devices. [import]uid: 199310 topic_id: 35591 reply_id: 141447[/import]

Thank you so much. Now everything works. I build for android and added the font in the folder. Also, added quotes. Thanks again:) [import]uid: 208318 topic_id: 35591 reply_id: 141450[/import]