添加延迟/定时器

我目前正在使用 Corona SDK,Lua 作为我的主要编程语言。

我正在针对以下代码遇到问题:当我运行它时,它会自动给我 'light' 的值,我设置 light = 2 并使用此循环,它应该每次将 light 减 1,直到 <= 0。当我运行程序时,值会同时显示为 1,0,-1。我想知道是否可以在每个值之间添加一个延迟。

我正在制作一个“西蒙说”游戏,因此箱子不亮起来,因为它同时运行所有内容。

以下是代码:

如果(count%20 == count- math.floor(count / 20)* 20then
   clicked = 0
   在光线> = 0 的同时 做如下操作
   Light = light- 1
   print(light)
   end
end
点赞
用户3739502
用户3739502

以下是适合您问题的示例代码。

请注意:我将代码基于您以上提供的代码。

这里是Corona SDK的一个简单的timer.performWithDelay函数。您可以在此处查看更多信息:https://docs.coronalabs.com/api/library/timer/performWithDelay.html

local lights = 2
local timerName -- 将计时器ID添加到您的计时器中,以便我们以后可以取消它

local function myFunction()

    lights =  lights - 1

    if (lights >= 0) then
        -- 对灯光进行操作
        print(lights)
    else

        -- 终止计时器
        timer.cancel( timerName )

    end

end

  if(count%20 == count - math.floor(count/20)*20) then

    timerName = timer.performWithDelay( 1000, myFunction, 0 )

  end
2015-07-08 05:06:03