如何在Lua中停止计分计时器....

时间:2013-09-09 14:40:11

标签: android timer lua corona

好的,这是我目前的得分 - 计时器

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()
    exp.x = bird.x
    exp.y = bird.y
    exp.isVisible = true
    exp:play()
    bird.isVisible = false
    timer.performWithDelay(1500, gameOver, 1)

之后游戏重定向到你死屏幕,其中得分应该是可见的,但我希望它回到0时

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

那么,我该怎么做?

2 个答案:

答案 0 :(得分:1)

试试这个

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

答案 1 :(得分:0)

代码不够明确,无法告诉我发生了什么,但如果你想忽略定时器的作用,请按以下方式执行:

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)

这种方式创建一个bool来检查何时禁用定时器,它不会结束定时器,但它只是使它不运行其他动作。每当你想关闭分数计时器时只需使用简单的真/假检查来打开/关闭它。

function explode()
    EnableScoreTimer = false