【问题标题】:How to stop score timer when .... in Lua如何在......在Lua中停止得分计时器
【发布时间】:2013-09-13 02:15:44
【问题描述】:

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

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

那么,我该怎么做呢?

【问题讨论】:

    标签: android timer lua coronasdk


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      代码不够清晰,无法告诉我发生了什么,但如果您试图忽略计时器的作用,请执行以下方式:

      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)
      

      这种方式会创建一个布尔值来检查何时禁用计时器,它不会结束计时器,但它只会使其不运行其他操作。每当您想关闭计分计时器时,只需使用简单的真/假检查即可打开/关闭它。

      function explode()
          EnableScoreTimer = false
      

      【讨论】:

      • 好的,但是计时器仍然停留在那里,然后在取消的时间上显示一个新的...
      • @user2735374 使用 Raymle 的答案是个好主意,我还没有使用过电晕,但这是 lua 上的一种正常方式,你可以很容易地得到一些东西,这种方法在计算机上非常有效,因为电脑的系统比手机好,所以在手机上放很多这样的系统不是个好主意。
      猜你喜欢
      • 2012-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多