Search code examples
timercoronasdkscoring

How can I put a timer in my game?


I have been trying to make a score keeper for my game in Corona SDK, but to no avail. How can I make a score keeper that adds one every second then saves the high score?


Solution

  • I normally don't just give solutions like this, for the future try to break your problem into smaller problems. Start with, how can I show text on my screen? Then go for, how can I update the text on my screen? And then start looking for ways to update every second. etc. Makes it a lot easier.

    -- Variables
    local score = 0
    local scoreTxt = display.newText( "Score: "..score, 100, 200, native.systemFont, 16 )
    scoreTxt:setFillColor( 1, 0, 0 )
    
    -- Listener for your timer, updates score variable and updates the text
    local function scoreKeeper( event )
        score = score + 1
        scoreTxt.text = "Score: "..score
    end
    
    -- Timer
    timer.performWithDelay( 1000, scoreKeeper, -1 )