How to create a timer

I’m trying to get my timer to appear on my top right hand side but I dont see it and I have no errors. Are there more solid tutorials on this?

I used a tut from youtube and its not working all that great:

I added My code:

-- setup timer here local output = display.newText ("0", 0, 0, native.systemFont, 320) output:setTextColor(0, 0, 0) output.anchorX = \_W \* 20 output.anchorY = \_H \* 0.09 local stateText = display.newText("Paused", 0, 0, native.systemFont, 16) stateText:setTextColor(255, 255, 255) stateText.x = \_W \* 0.5 stateText.y = \_H + 10 --Create method to add the settext method to text objects local function addSetText(obj) -- create a cudtom method for setting text function obj:setText(txt, align) local a = align or display.CenterReferencePoint local oldX = self.x local oldY = self.y self.text = txt self:setReferencePoint(a) self.x = oldX self.y = oldY end end -- Add the setText method to the two text objects addSetText(output) addSetText(stateText) local tmr tmr = timer.performWithDelay(500, function(e) output:setText(e.count) if(e.count == 10) then timer.cancel(tmr) tmr = nil stateText:setText("Timer Finished") end end, 10) timer.pause(tmr) local paused = true local function touchHandler(e) if(tmr) then if(e.phase == "ended" or e.phase == "cancelled") then if(paused == false) then timer.pause(tmr) stateText:setText("Paused") paused = true elseif(pauseed == true) then timer.resume(tmr) stateText:setText("Running") paused = false end end end end Runtime:addEventListener("touch", touchHandler)

I gotta admit this example really sucks is there something more quick and easier to implement. I want the timer to start on touch

What do _W and _H represent in your code? I commonly see them used for display.contentWidth and display.contentHeight, but if that’s the case I don’t understand what this part is doing:

output.anchorX = \_W \* 20 output.anchorY = \_H \* 0.09

That would put the anchorX really really far away from the object itself (although by default it caps at 1 so this would be redundant). 

Did you mean to just do .x and .y rather than .anchorX and .anchorY?

[quote name=“Alan QuizTix” post=“338967” timestamp=“1473840869”]What do [color=#111111]_W and _H represent in your code? I commonly see them used for display.contentWidth and display.contentHeight, but if that’s the case I don’t understand what this part is doing:[/color]

output.anchorX = \_W \* 20 output.anchorY = \_H \* 0.09

That would put the anchorX really really far away from the object itself (although by default it caps at 1 so this would be redundant).    Did you mean to just do .x and .y rather than .anchorX and .anchorY?[/quote] Its a constant of width and hieght in my main function but you just struck a light bulb in my head. May i can get rid of those constants .

I think it is something with the anchors. I would prefer to keep it either 0 or 1, but it can be other values.

Try setting the text like this. This should make it on the top-right corner.

 local output = display.newText ("0", 0, 0, native.systemFont, 320) output:setTextColor(0, 0, 0) output.anchorX = 1 output.anchorY = 0 output.x = display.viewableContentWidth output.y = display.screenOriginY local stateText = display.newText("Paused", 0, 0, native.systemFont, 16) stateText:setTextColor(255, 255, 255) stateText.x = 1 stateText.y = 0 stateText.x = output.x stateText.y = output.y + output.contentHeight + stateText.contentHeight

That works, thanks

What do _W and _H represent in your code? I commonly see them used for display.contentWidth and display.contentHeight, but if that’s the case I don’t understand what this part is doing:

output.anchorX = \_W \* 20 output.anchorY = \_H \* 0.09

That would put the anchorX really really far away from the object itself (although by default it caps at 1 so this would be redundant). 

Did you mean to just do .x and .y rather than .anchorX and .anchorY?

[quote name=“Alan QuizTix” post=“338967” timestamp=“1473840869”]What do [color=#111111]_W and _H represent in your code? I commonly see them used for display.contentWidth and display.contentHeight, but if that’s the case I don’t understand what this part is doing:[/color]

output.anchorX = \_W \* 20 output.anchorY = \_H \* 0.09

That would put the anchorX really really far away from the object itself (although by default it caps at 1 so this would be redundant).    Did you mean to just do .x and .y rather than .anchorX and .anchorY?[/quote] Its a constant of width and hieght in my main function but you just struck a light bulb in my head. May i can get rid of those constants .

I think it is something with the anchors. I would prefer to keep it either 0 or 1, but it can be other values.

Try setting the text like this. This should make it on the top-right corner.

 local output = display.newText ("0", 0, 0, native.systemFont, 320) output:setTextColor(0, 0, 0) output.anchorX = 1 output.anchorY = 0 output.x = display.viewableContentWidth output.y = display.screenOriginY local stateText = display.newText("Paused", 0, 0, native.systemFont, 16) stateText:setTextColor(255, 255, 255) stateText.x = 1 stateText.y = 0 stateText.x = output.x stateText.y = output.y + output.contentHeight + stateText.contentHeight

That works, thanks