Lua/Corona SDK:为什么我的倒计时停止了?

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

-- 创建按钮
local blueButton = display.newCircle (160,240,45)
blueButton:setFillColor(0,.5,1)

-- 创建点击计数器
local number = 0
local textField = display.newText(number, 160, 30, native.systemFont, 52)

-- 创建倒计时计时器
local count = 20
local textCount = display.newText(count, 160, 70, native.systemFont, 52)
textCount:setFillColor(0,1,.25)

-- 创建倒计时函数
local function countDown()
    count = count - 1
    textCount:removeSelf()
    textCount = display.newText(count, 160, 70, native.systemFont, 52)
    textCount:setFillColor(0,1,.25)
end

-- 创建点击函数
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

-- 点击按钮调用 tap 函数
blueButton:addEventListener("tap", buttonTap)

-- 每秒倒计时
timer.performWithDelay(1000, countDown, count)
点赞
用户7026995
用户7026995

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

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

有关计时器的更多信息,请参阅[文档](https://docs.coronalabs.com/api/library/timer/index.html)。

尝试

-- 创建按钮
local blueButton = display.newCircle(160, 240, 45)
blueButton:setFillColor(0, .5, 1)

-- 创建 Tap 计数器
local number = 0
local textField = display.newText(number, 160, 30, native.systemFont, 52)

-- 创建倒计时器
local count = 20
local textCount = display.newText(count, 160, 70, native.systemFont, 52)
textCount:setFillColor(0, 1, .25)

local myTimer

-- 创建倒数计算函数
local function countDown()
    count = count - 1
    textCount.text = count
    if (count < 1) then -- 如果 true 则 count = 0
        timer.cancel(myTimer)
    end
end

-- 创建 Tap 函数
local function buttonTap(event)
    number = number + 1
    textField.text = number

    count = count + 1
    textCount.text = count
end

-- 点击按钮调用 tap 函数
blueButton:addEventListener("tap", buttonTap)

-- 每秒进行倒数
myTimer = timer.performWithDelay(1000, countDown, -1)
2017-05-10 22:34:10