简体   繁体   English

…在Lua中如何停止比分计时器

[英]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 之后,游戏会重定向到您的“死亡”屏幕,在该屏幕上应该可以看到分数,但是我希望在出现以下情况时将其恢复为0

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

So, how do I do that? 那么,我该怎么做呢?

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

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. 这种方法创建了一个bool来检查何时禁用计时器,它不会终止计时器,但只会使其不运行其他操作。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM