Okay, for context, I am quite new to both Solar2d/Corona and the Lua programming language, I do have decent experience in C, C++, Java, and Python. I’m currently making an exercise app. As part of that app, I was hoping to have a timer for each exercise. The timer’s time would reset after each exercise is done to go the next one. I have been trying to get this to work for a ridiculously long amount of time. I have tried countless things, and am kind of desperate at this point for help or anything that could help point me in the right direction. Here is the code (excluding parts which aren’t relevant) and an explanation for the code below it:
local composer = require( "composer" )
local scene = composer.newScene()
local background
local function sleep(s)
local ntime = os.clock() + s/10
repeat until os.clock() > ntime
end
local function updateTime(secondsLeft, clockText, it)
while (secondsLeft >= 0) do
sleep(10)
secondsLeft = secondsLeft - 1
clockText.text = secondsLeft
end
if (it ~= 0) then
updateTime(20,clockText,it-1)
end
end
function scene:create( event )
local sceneGroup = self.view
local para = event.params.var1
it = event.params.var2
local texty = event.params.var3
background = display.newImageRect(sceneGroup,"images/whiteBackground.jpg",768,1500)
background.x = display.contentCenterX
background.y = display.contentCenterY
local secondsLeft = 20
local it = 3
local clockText = display.newText( secondsLeft, display.contentCenterX, 80, native.systemFont, 72 )
clockText:setFillColor( 0.7, 0.7, 1 )
local nextButton = display.newText(sceneGroup, "Regular", 500, 1300, nativeSystemFont, 60)
nextButton:setFillColor(0,0,256)
nextButton:addEventListener("tap", function ()
updateTime(secondsLeft, clockText, it)
end)
end
scene:addEventListener("create",scene)
collectgarbage()
return scene
I start off by defining a text object, clockText. This is to display how many seconds are left, acting as the visual representation of the counter. I have a button that starts the process by calling the updateTime function. Three variables are passed to the updateTime function: secondsLeft, the number of seconds that the timer will run for, clockText, the text object that will display the time left, it, the number of iterations the timer will run for (the number of exercises that will be ran).
In the updateTime function itself, there is a while loop that subtracts one from the secondsLeft and updates the text of the clockText to be equal to the new value of secondsLeft. As well, there is a call to a sleep function, which makes the program wait for k seconds, in this case one. After the while loop is finished, an if statement checks if it (the number of iterations left) is 0. If not, the function is called again with it subtracted by 1.
Currently, the text of clockText updates when I print it to the console, but the new text doesn’t show up on the actual app’s screen. Only when the function call has ended does the text of clockText update on the app itself. The best I’ve gotten is for the text to update on the screen for one iteration, but break when I try to get it to do more than one in a row. And I don’t even remember how I got that. If any of this sounds confusing at all, then sorry I just have been working on this for quite a while and haven’t gotten much sleep, I’d be happy to clarify anything. Any and all help is very appreciated.