Nicholas O'Neal
Nicholas O'Neal

Reputation: 21

How to update text on a loop in Lua/Solar2d

I'm trying to make a part of a program that's a timer through two loops. The timer will repeat a certain number of times (the it variable) hence the first loop. The second loop is the actual timer. For a certain number of seconds, every second it updates the text of a text object. The problem I face is that when I print the text of the text object to the console, it updates, but the text won't change on the screen itself until all the loops are done. Any help with explanation or anything to help guide me in the right direction would be very much appreciated.

Here is the code, don't worry about the unused parameters:

    local function sleep(s)
        local ntime = os.clock() + s/10
        repeat until os.clock() > ntime
    end
    
    local function watch(mode, it, text, texty, para)
        local text = display.newText(scene.view, "", 
    display.contentCenterX, 200, nativeSystemFont, 60)
        text:setFillColor(0,0,256)

        local i = 0
        local sec = 0
        local goal = 20
        
        text.text = sec
        while (i < it) do
            sec = 0
            while (sec < goal) do
                sec = sec + 1
                print(text.text)
                text.text = sec
                sleep(10)
            end
            i = i + 1
        end
    end

Upvotes: 0

Views: 198

Answers (0)

Related Questions