【问题标题】:Lua/Corona SDK: Why is my countdown stopping?Lua/Corona SDK:为什么我的倒计时停止了?
【发布时间】:2017-10-09 17:01:39
【问题描述】:

我正在编写一个应用程序,当您点击一个按钮时,它会增加倒数计时器的剩余时间,并增加计数器上的数字,以显示您已进行了多少次点击。我的问题是,当两个数字相等时,倒计时停止并且数字和计数器成为同义词。我需要改变什么/我做错了什么?

-- Create Button
local blueButton = display.newCircle (160,240,45)
blueButton:setFillColor(0,.5,1)

-- Create Tap-counter
local number = 0
local textField = display.newText(number, 160, 30, native.systemFont, 52)

-- Create Countdown Timer
local count = 20
local textCount = display.newText(count, 160, 70, native.systemFont, 52)
textCount:setFillColor(0,1,.25)

-- Create countdown function
local function countDown()
    count = count - 1
    textCount:removeSelf()
    textCount = display.newText(count, 160, 70, native.systemFont, 52)
    textCount:setFillColor(0,1,.25)
end


-- Create tap function
local function buttonTap(event)
    number = number + 1
    textField:removeSelf()
    textField = display.newText(number, 160, 30, native.systemFont, 52)

    count = count + 1
    textCount:removeSelf()
    textCount = display.newText(count, 160, 70, native.systemFont, 52)
    textCount:setFillColor(0,1,.25)
end


-- Tapping button calls tap function 
blueButton:addEventListener("tap", buttonTap)

-- countdown every second
timer.performWithDelay(1000, countDown, count)

【问题讨论】:

    标签: android ios lua coronasdk


    【解决方案1】:

    如果你想倒计时到 0,你需要设置无限次的定时器迭代。这就是为什么我使用 -1 作为计时器的最后一个参数。

    其次,您不需要在每次迭代后创建新的文本对象。只需更改上面的文字即可。

    您可以在documentation 中找到有关计时器的更多信息。

    试试

    -- Create Button
    local blueButton = display.newCircle (160,240,45)
    blueButton:setFillColor(0,.5,1)
    
    -- Create Tap-counter
    local number = 0
    local textField = display.newText(number, 160, 30, native.systemFont, 52)
    
    -- Create Countdown Timer
    local count = 20
    local textCount = display.newText(count, 160, 70, native.systemFont, 52)
    textCount:setFillColor(0,1,.25)
    
    local myTimer
    
    -- Create countdown function
    local function countDown()
        count = count - 1
        textCount.text = count
        if ( count < 1 ) then -- so count = 0 if true
            timer.cancel( myTimer )
        end 
    end
    
    
    -- Create tap function
    local function buttonTap(event)
        number = number + 1
        textField.text = number
    
        count = count + 1
        textCount.text = count
    end
    
    
    -- Tapping button calls tap function 
    blueButton:addEventListener("tap", buttonTap)
    
    -- countdown every second
    myTimer = timer.performWithDelay(1000, countDown, -1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-31
      • 2016-04-09
      • 2017-08-24
      • 2013-06-04
      • 1970-01-01
      • 2013-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多