user2735374
user2735374

Reputation: 43

How to stop score timer when .... in Lua

Ok, so this is my current score - timer

local scoreTxt = display.newText( "Score: 0", 0, 0, "Helvetica", 40 )
scoreTxt:setReferencePoint(display.TopLeftReferencePoint)
scoreTxt.x = display.screenOriginX + 10
scoreTxt.y = display.screenOriginY + 32

local function updateScore()
     score = score + 20
     scoreText.text = string.format("Score: %d", score)
end

local scoreTimer = timer.performWithDelay(1000, updateScore, 0)

I want that timer to pause when

function explode()
    exp.x = bird.x
    exp.y = bird.y
    exp.isVisible = true
    exp:play()
    bird.isVisible = false
    timer.performWithDelay(1500, gameOver, 1)

After that the game redirects to you died screen, where the score should be visible, but I want it to go back to 0 when

function start(event)
    if event.phase == "began" then
        storyboard.gotoScene("game", "fade", 50)
    end
end

So, how do I do that?

Upvotes: 0

Views: 1577

Answers (2)

NaviRamyle
NaviRamyle

Reputation: 4007

Try this

score = 0 -- You need to set the score to 0 everytime you create the game scene
local scoreTxt = display.newText( "Score: 0", 0, 0, "Helvetica", 40 )
scoreTxt:setReferencePoint(display.TopLeftReferencePoint)
scoreTxt.x = display.screenOriginX + 10
scoreTxt.y = display.screenOriginY + 32

local function updateScore()
     score = score + 20
     scoreText.text = string.format("Score: %d", score)
end

local scoreTimer = timer.performWithDelay(1000, updateScore, 0)

function explode()
    timer.cancel(scoreTimer) --This will cancel the timer and eventually stop
    ...
end

Upvotes: 1

111WARLOCK111
111WARLOCK111

Reputation: 867

The code Is not clear enough to tell me what's going on, But If you're trying to Ignore what timer does, Do the following way:

EnableScoreTimer = true -- Make It Global so you can call It from other files too.

local function updateScore()
     if not EnableScoreTimer then return end
     score = score + 20
     scoreText.text = string.format("Score: %d", score)
end

local scoreTimer = timer.performWithDelay(1000, updateScore, 0)

This way creates a bool to check when to disable the timer or not, It doesn't end the timer but It just makes It to not run the other actions. Whenever you want to turn the score timer off Just use a simple true/false check to turn It on / off.

function explode()
    EnableScoreTimer = false

Upvotes: 0

Related Questions